inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
76 stars 21 forks source link

non parameter estimator cause error #92

Closed ZSRNOG closed 3 years ago

ZSRNOG commented 3 years ago

I run the example provided by bru() function in inlabru document `# An important addition to the INLA methodology is bru's ability to use

non-linear predictors. Such a predictor can be formulated via like()'s

\code{formula} parameter. For instance

if (require("INLA", quietly = TRUE)) { input.df <- data.frame(x=cos(1:10)) z = 2 input.df <- within(input.df, y <- 5 + exp(z)x + rnorm(10, mean=0, sd=0.1)) lik = like(family = "gaussian", data = input.df, formula = y ~ exp(z)x + Intercept, E = 10000) fit <- bru( ~ z + Intercept, lik)

Check the result (z posterior should be around 2)

fit$summary.fixed } ` the result is image

the posterior of $z$ is 0.999, which is far from 2

finnlindgren commented 3 years ago

Thanks for noticing this issue! The notation in the example was unsafe; Model components like +z are interpreted as covariate fixed effects, and it found the z in the global environment, preventing it from treating it as the intended individual latent variable, beta_z that saw intended. The predictor expression was interpreted as exp(z * beta_z) * x instead of the intended exp(beta_z) * x. The safe notation is ~z(1)+Intercept(1) that makes the intended behaviour explicit. Many old examples still use the unsafe notation for "intercept-like" components without including the explicit (1) part; We'll go through and get these corrected, but they're unfortunately scattered to many places so it will take some time to find and fix them all.

finnlindgren commented 3 years ago

The corrected example:

z = 2
input.df <- within(input.df, y <- 5 + exp(z)*x + rnorm(10, mean=0, sd=0.1))
lik = like(family = "gaussian", data = input.df,
            formula = y ~ exp(z)*x + Intercept)
fit <- bru( ~ z(1) + Intercept(1), lik)

# Check the result (z posterior should be around 2)
fit$summary.fixed
#               mean          sd 0.025quant 0.5quant 0.975quant     mode          kld
# z         1.997771 0.003796729   1.990273 1.997771   2.005264 1.997771 4.350033e-05
# Intercept 5.035406 0.015194441   5.005034 5.035406   5.065733 5.035407 1.237536e-04
ZSRNOG commented 3 years ago

Dear finnlindgen: I still have an error when I run your code on Win10 with R 4.03 image

ZSRNOG commented 3 years ago

Dear finnlindgen:

I correct this error with following code

image

But, when I use expression z(1) + Interpect(1), same error occurs image

finnlindgren commented 3 years ago

What's the sessionInfo() output?

ZSRNOG commented 3 years ago

the sessionInfo is

image

finnlindgren commented 3 years ago

I need the sessionInfo when inlabru and INLA are loaded.

ZSRNOG commented 3 years ago

OK, finnlindgren , is it this?

image

finnlindgren commented 3 years ago

OK, thanks! From what I recall, the current CRAN release that you have of inlabru, 2.1.13, didn't fully support the safer syntax z(1), which is why the safe syntax fail on your system. The only workaround is then to make sure that any "automatic intercept" parameter components use unique names that aren't present in the workspace. The development version, currently 2.1.14.901, supports the safe syntax, and, and also gives a warning if it detects a problem (but since z was in the workspace it wouldn't be able to detect the problem in the example case). The development version is under heavy development at the moment, and the tutorial materials haven't yet been updated to match, so if you can make it work with the current CRAN version you don't need to upgrade yet.