1.4 Transformations
We may want to simulate predictors that are not normally distributed. Although the underlying simulation procedure assumes multivariate normality, the predictors can be transformed, before they are multiplied by the beta values. To do this we can provide the transformation function to the functions option of a given parameter list, as a character vector. The given function needs to be a known function in R. The below code will exponentiate rainfall (using the exp
function), before it is scaled by its beta (here 2).
<- simulate_population(
squid_data n=2000,
response_name = "body_mass",
parameters=list(
observation=list(
names=c("temperature","rainfall"),
functions=c(NA,"exp"),
beta = c(0.5,0.3)
),residual=list(
vcov=0.3
)
)
)
<- get_population_data(squid_data)
data head(data)
## body_mass temperature rainfall residual squid_pop
## 1 0.8808384 0.9661316 1.7834867 -0.1372734 1
## 2 0.2263617 -0.3593466 1.8415211 -0.1464213 1
## 3 0.3550851 1.5984683 0.2318093 -0.5136918 1
## 4 1.4382651 -1.5338100 4.4990114 0.8554666 1
## 5 0.6630719 2.1241124 0.9072182 -0.6711498 1
## 6 0.6877911 -0.9466144 0.9671257 0.8709606 1
hist(data$rainfall, xlab="Rainfall",main="", breaks=100)
If a covariance between variables is specified, this covariance is on the untransformed (Gaussian) scale (as the variables are simulated as multivariate normal), NOT on the transformed scale, so care should be taken with this. For example:
<- simulate_population(
squid_data n=2000,
response_name = "body_mass",
parameters=list(
observation=list(
names=c("temperature","rainfall"),
vcov=matrix(c(1,0.7,0.7,1), nrow=2,byrow=TRUE),
functions=c(NA,"exp"),
beta = c(0.5,0.3)
),residual=list(
vcov=0.3
)
)
)
<- get_population_data(squid_data)
data
cov(data$temperature,data$rainfall)
## [1] 1.132242
cov(data$temperature,log(data$rainfall))
## [1] 0.6947967
The simulated covariance can be recovered on the back-transformed predictor.
The simulated_variance()
function will also no longer be accurate, as the calculations are based on variables on the untransformed scale.