yes
> For exemple, if I want to include a random slope in my model for a
> contious variable "x" that varies in different "region".
>
> With the package LME4, a code like this would do the job:
>
> y ~ x + (0+x|region)
>
> If I wanted to include an uncorrelated random intercept, it will
> become y ~ x + (1|region) + (0+x|region)
>
> Could I reproduce these models with INLA? if yes, how?
one example is here
http://www.r-inla.org/models/tools#TOC-Copying-a-model
you need to copy a model, and then its like normal.
just make sure you get the ordering of the indices correct in the iid2d
model; see
http://www.r-inla.org/models/latent-models
and the iid2d model.
hope this helps.
H
--
Håvard Rue
Department of Mathematical Sciences
Norwegian University of Science and Technology
N-7491 Trondheim, Norway
Voice: +47-7359-3533 URL : http://www.math.ntnu.no/~hrue
Fax : +47-7359-3524 Email: havar...@math.ntnu.no
This message was created in a Microsoft-free computing environment.
I presumed you ment that you wanted to correlated the intercept and the
slope, but if you do not want that, then
f(region, covariate, model ="iid")
is a random slope model, where
region[i] is the region for data y[i]
covariate[i] is the covariate for data y[i]
so that this gives with z = covariate,
+ \beta_region * z_i ....
the second argument in f() is an optional weight for the f-term.
Thank you Havard for your previous response. It is now clear to me how to code a random slope model. However, I had trouble to obtain convergence with a simple random intercept model (see below) whereas it worked fine using lme4 package (see below). Also, I have tried to code a combined random intercept-slope model (see below) in INLA using the copy function that you mentioned in your previous message but I did not have any success. Could you indicate how to replicate the following models (fitted with lme4 packages) using the simulated data set below?
Thank you for your time,
Julien
########################
## Simulated data set ##
########################
x <- sort(unique(runif(100, 0, 20)))
y1 <- 60 - 2*x + rnorm(length(x), 0, 4)
y2 <- 70 - 0.6*x + rnorm(length(x), 0, 4)
y3 <- 45 + 1*x + rnorm(length(x), 0, 4)
y4 <- 35 + 1.3*x + rnorm(length(x), 0, 4)
plot(x,y1, ylim=c(20,80), xlim=c(0,30))
points(x, y2, col="red")
points(x, y3, col="green")
points(x, y4, col="orange")
abline(a=coef(lm(y1~x))[1], b=coef(lm(y1~x))[2])
abline(a=coef(lm(y2~x))[1], b=coef(lm(y2~x))[2])
abline(a=coef(lm(y3~x))[1], b=coef(lm(y3~x))[2])
abline(a=coef(lm(y4~x))[1], b=coef(lm(y4~x))[2])
mydata <- data.frame(x1 = rep(x, times = 4), y = c(y1,y2,y3,y4), bloc = rep(c(1,2,3,4), each = length(x)), fuzzy = rep(1, 400))
##########
## LME4 ##
##########
require(lme4)
# no random effect
model_no_random <- lmer( y ~ x1 + (1|fuzzy), data = mydata)
summary(model_no_random)
# random intercept only
model_intercept <- lmer( y ~ x1 + (1|bloc), data = mydata)
summary(model_intercept)
# random slope only
model_slope <- lmer( y ~ x1 + (0 + x1|bloc), data = mydata)
summary(model_slope)
# random slope of x1 within bloc with correlated intercept
model_slope_intercept_correlated <- lmer( y ~ x1 + (x1|bloc), data = mydata)
summary(model_slope_intercept_correlated)
# uncorrelated random intercept and random slope within bloc
model_slope_intercept_uncorrelated <- lmer(y ~ x1 + (1|bloc) + (0 + x1|bloc), data = mydata)
summary(model_slope_intercept_uncorrelated)
##########
## INLA ##
##########
require(INLA)
# no random effect
formule_no_random = y ~ x1
model_no_random_INLA = inla.cpo(formula=formule_no_random, data=mydata, family="gaussian", control.compute=list(dic=TRUE))
summary(model_no_random_INLA)
# random intercept only
formule_random_intercept_only = y ~ x1 + f(bloc, model = "iid")
model_random_intecept_only_INLA = inla.cpo(formula=formule_random_intercept_only, data=mydata, family="gaussian", control.compute=list(dic=TRUE))
summary(model_random_intecept_only_INLA)
# random slope only
formule_random_slope_only = y ~ x1 + f(bloc, x1, model = "iid")
model_random_slope_only_INLA = inla.cpo(formula=formule_random_slope_only, data=mydata, family="gaussian", control.compute=list(dic=TRUE))
summary(model_random_slope_only_INLA)
# random slope of x1 within bloc with correlated intercept
formule_random_correlated_intercept_slope = y ~ x1 + ?
model_random_correlated_intercept_slope_INLA = inla.cpo(formula=formule_random_correlated_intercept_slope, data=mydata, family="gaussian", control.compute=list(dic=TRUE))
summary(model_random_correlated_intercept_slope_INLA)
# uncorrelated random intercept and random slope within bloc
formule_random_uncorrelated_intercept_slope = y ~ x1 + ?
model_random_uncorrelated_intercept_slope_INLA = inla.cpo(formula=formule_random_uncorrelated_intercept_slope, data=mydata, family="gaussian", control.compute=list(dic=TRUE))
summary(model_random_uncorrelated_intercept_slope_INLA)
I think this is what you want. I hope I have understoof the models
correct; I'm not familiar to the (x1|bloc) notation...
results will surely will prior dependent.
also, I needed to make the overall intercept have a proper prior using
prec.intercept=0.0001 (default 0).
Best
H
********************************
x <- sort(unique(runif(100, 0, 20)))
y1 <- 60 - 2*x + rnorm(length(x), 0, 4)
y2 <- 70 - 0.6*x + rnorm(length(x), 0, 4)
y3 <- 45 + 1*x + rnorm(length(x), 0, 4)
y4 <- 35 + 1.3*x + rnorm(length(x), 0, 4)
plot(x,y1, ylim=c(20,80), xlim=c(0,30))
points(x, y2, col="red")
points(x, y3, col="green")
points(x, y4, col="orange")
abline(a=coef(lm(y1~x))[1], b=coef(lm(y1~x))[2])
abline(a=coef(lm(y2~x))[1], b=coef(lm(y2~x))[2])
abline(a=coef(lm(y3~x))[1], b=coef(lm(y3~x))[2])
abline(a=coef(lm(y4~x))[1], b=coef(lm(y4~x))[2])
mydata <- data.frame(x1 = rep(x, times = 4),
y = c(y1,y2,y3,y4),
bloc = rep(c(1,2,3,4),
each = length(x)),
fuzzy = rep(1, 400))
require(lme4)
require(INLA)
## no random effect
##
model_no_random <- lmer( y ~ x1 + (1|fuzzy), data = mydata)
summary(model_no_random)
formule_no_random = y ~ x1
model_no_random_INLA = inla.cpo(formula=formule_no_random,
data=mydata, family="gaussian", control.compute=list(dic=TRUE))
summary(model_no_random_INLA)
## random intercept only
##
model_intercept <- lmer( y ~ x1 + (1|bloc), data = mydata)
summary(model_intercept)
formule_random_intercept_only = y ~ x1 + f(bloc, model = "iid")
model_random_intecept_only_INLA =
inla.cpo(formula=formule_random_intercept_only,
data=mydata, family="gaussian", control.compute=list(dic=TRUE),
control.fixed = list(prec.intercept = 0.0001))
summary(model_random_intecept_only_INLA)
## random slope only
##
model_slope <- lmer( y ~ x1 + (0 + x1|bloc), data = mydata)
summary(model_slope)
formule_random_slope_only = y ~ x1 + f(bloc, x1, model = "iid")
model_random_slope_only_INLA =
inla.cpo(formula=formule_random_slope_only,
data=mydata, family="gaussian", control.compute=list(dic=TRUE),
control.fixed = list(prec.intercept = 0.0001))
summary(model_random_slope_only_INLA)
## random slope of x1 within bloc with correlated intercept
##
model_slope_intercept_correlated <- lmer( y ~ x1 + (x1|bloc), data =
mydata)
summary(model_slope_intercept_correlated)
n.block = max(mydata$block) ## = 4
mydata$i.intercept = mydata$bloc
mydata$j.intercept = mydata$bloc + n.block ## see doc for iid2d
formule_random_correlated_intercept_slope = y ~ x1 +
f(i.intercept, model="iid2d", n=2*n.block) +
f(j.intercept, x1, copy="i.intercept")
model_random_correlated_intercept_slope_INLA = inla.cpo(
formula=formule_random_correlated_intercept_slope,
data=mydata, family="gaussian", control.compute=list(dic=TRUE))
summary(model_random_correlated_intercept_slope_INLA)
## uncorrelated random intercept and random slope within bloc
##
model_slope_intercept_uncorrelated <- lmer(y ~ x1 + (1|bloc) + (0 + x1|
bloc), data = mydata)
summary(model_slope_intercept_uncorrelated)
formule_random_uncorrelated_intercept_slope = y ~ x1 +
f(i.intercept, model="iid") + f(j.intercept, x1, model="iid")
model_random_uncorrelated_intercept_slope_INLA =
inla.cpo(formula=formule_random_uncorrelated_intercept_slope,
data=mydata, family="gaussian", control.compute=list(dic=TRUE),
control.fixed = list(prec.intercept = 0.0001))
summary(model_random_uncorrelated_intercept_slope_INLA)
A B C and D are factors with one coof for one level; sorry.
I wrote down the model as I understood it; its in the attachment.
Here is how to implement it:
data$A = as.numeric(data$A)
data$B = as.factor(data$B)
data$C = as.factor(data$C)
data$D = as.factor(data$D)
formula = y ~ 1 + A + B:A + C:A + D:A
## since A is numeric and B is a factor, it will expand into
## \beta_{B_i}^B * A_i
## small test-example
y = 1:100
B = as.factor(rep(1:5, each = 20))
A = as.numeric(y)
formula = y ~ 1 + B:A
r = inla(formula, data = data.frame(y,A,B))
## so that for each of the 5 \beta_{B}'s, one for each factor, should
## be 1.
> r$summary.fixed[,"mean"]
(Intercept) B1:A B2:A B3:A
B4:A
5.293578909e-10 1.000000000e+00 1.000000000e+00 1.000000000e+00
1.000000000e+00
B5:A
1.000000000e+00
best
H