# Unscaling the result of a calculation

Hi! I have been working with a set of data that I wanted to calculate a Hypervolume from. After succesfully doing so, scaled it and calculated its centroid. Now I am trying to do the reverse of scaling on the centroid result so I can compare it to another one, but I do not know how to proceed. I have tried using the apply function but I am quite unfamiliar with it, so I am not doing it right. Here is my relevant code:

dataCeltisaustralis <- df.clim.occ_wider %>% filter(., species == "Celtis australis") %>% select(mean_temperature, mean_precipitation, seasonality_temperature, seasonality_precipitation)
scaledCeltisaustralis<- scale(dataCeltisaustralis)
hv_scaled_celtisaustralis<- hypervolume(scaledCeltisaustralis)
centroid_Celtisaustralis <- get_centroid(hv_scaled_celtisaustralis)
unscaledCentroid_Celtisaustralis <- apply(centroid_Celtisaustralis, 1:4, function(x) x*sd, x+mean)

Should I use a different function than "apply" here? Or is it just my application of it that is wrong? Thanks a lot.

I do not see where you stored values for sd and mean that are used in your last apply() function. I do not know what kind of object centroid_Celtisaustralis is, so the code below may need to be adjusted.
Here is an example of calculating with scaled data and reversing the scaling to get the equivalent answer with the unscaled data. Notice that I stored the mean and standard deviation of each column so I could reverse the scaling.

``````DF <- data.frame(X1 = c(1,2,4,4,5), X2 = c(13,15,23,9,34))
Means <- colMeans(DF)
Sds <- sapply(DF,sd)

UnscaledCalc <- sapply(DF,function(V) median(V))
UnscaledCalc
#> X1 X2
#>  4 15

DFscld <- scale(DF)

ScaledCalc <- apply(DFscld, MARGIN = 2, function(V) median(V))
ScaledCalc
#>         X1         X2
#>  0.4868645 -0.3834669

RevScaledCalc <- mapply(function(X, Mu, SD) X * SD + Mu, ScaledCalc, Means, Sds)
RevScaledCalc
#> X1 X2
#>  4 15
``````

Created on 2022-11-21 with reprex v2.0.2

Then that is probably my problem, since I have not stored the values of mean and sd for each column. Would it be possible to adjust this code so that it works with the columns of a dataframe? Thank you.

``````Means <- colMeans(dataCeltisaustralis)
SDs   <- sapply(dataCeltisaustralis, sd)
``````

and then post the output of these three calls to dput()

``````dput(Means)
dput(SDs)
dput(centroid_Celtisaustralis)
``````

Put a line with three back ticks before and after each block of output, like this
```
output of dput(centroid_Celtisaustralis)
```

Understood. I will try it out. Thank you very much.

``````c(mean_temperature = 13.0508405674843, mean_precipitation = 81.220345786541,
seasonality_temperature = 0.560144794813539, seasonality_precipitation = 0.724047400179261
)
``````
``````c(mean_temperature = 2.8309422735853, mean_precipitation = 30.4522029093035,
seasonality_temperature = 0.822175114605129, seasonality_precipitation = 0.172450681104083
)
``````
``````c(mean_temperature = -0.128170016005294, mean_precipitation = 0.147130553149461,
seasonality_temperature = 0.0588307335794767, seasonality_precipitation = 0.0482962633828218
)
``````

Is this what you are after?

``````Means <- c(mean_temperature = 13.0508405674843, mean_precipitation = 81.220345786541,
seasonality_temperature = 0.560144794813539,
seasonality_precipitation = 0.724047400179261)

SDs <- c(mean_temperature = 2.8309422735853, mean_precipitation = 30.4522029093035,
seasonality_temperature = 0.822175114605129,
seasonality_precipitation = 0.172450681104083)

centroid_Celtisaustralis <- c(mean_temperature = -0.128170016005294, mean_precipitation = 0.147130553149461,
seasonality_temperature = 0.0588307335794767,
seasonality_precipitation = 0.0482962633828218)

RevScaled <- centroid_Celtisaustralis * SDs + Means
RevScaled
#>          mean_temperature        mean_precipitation   seasonality_temperature
#>                12.6879987                85.7007952                 0.6085140
#> seasonality_precipitation
#>                 0.7323761
``````

Created on 2022-11-22 with reprex v2.0.2

Yes it is. I had tried something similar to that line before but it did not work. I have tried again and now it does. I must have made a mistake along the way. Thank you very much.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.