I believe I am making a mistake in parametrization in this case. My goal is fit a lognormal model to data using gamlss
in R, then simulate from that fitted model. Here's an example where the mean and standard deviation vary by the variable x.
library(gamlss)
#create dataset
df <- data.frame(x = rep(0:9,1000),
mean = rep(c(6900,6900,7000,7600,7200,7900,7900,8100,8500,8800), 1000),
s = rep(c(43400,40200,36700,94200,31100,50600,45600,43600,53300,38400), 1000)) %>%
rowwise() %>%
mutate(y = rlnorm(1, log(mean^2 / sqrt(s^2 + mean^2)), sqrt(log(1 + (s^2 / mean^2))))) %>%
ungroup()
# fit model
df_gam <- gamlss(y ~ cs(x, df = 4),
sigma.formula =~ x,
data = df,
family = LOGNO(),
#method = CG(),
trace = TRUE)
I'll use x = 2 as an example for my confusion. We get
> df_gam$mu.fv[3]
[1] 7.193461
> df_gam$sigma.fv[3]
1.93269
Now I want to simulate y using those parameters.
sim_yhat <- (rlnorm(1e6, df_gam$mu.fv[3], df_gam$sigma.fv[3]))
But if we look at the mean and std on the response scale again, it doesn't make sense. We expect approximately 7000 and 36700, respectively, but we get
> mean(sim_yhat)
[1] 8613.995
> sqrt(var(sim_yhat))
[1] 54731.78
I believe I am missing a transformation of some sort when using the values back in rlnorm but I cannot figure out what.
Using gamlss_5.1-6
and R 4.0.0
df
, I definedmean = 7000
ands = 36700
forx = 2
. My expectation is that the using the parameters from the gamlss model and simulating random deviations from that will result in roughly the same mean and standard deviation (given some error of course). But my simmed values do not match up. I used this reference $\endgroup$sig
is undefined). $\endgroup$sig
should bes
. Fixed. I definitely cannot guarantee the generation is correct. Testing withsim <- rlnorm(1e6, log(7000^2 / sqrt(36700^2 + 7000^2)), sqrt(log(1 + (36700^2 / 7000^2))))
returns amean
near 7000 andsqrt(var())
near 36700 though $\endgroup$