Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`nlme` with crossed random effects

I am trying to fit a crossed non-linear random effect model as the linear random effect models as mentioned in this question and in this mailing list post using the nlme package. Though, I get an error regardless of what I try. Here is an example

library(nlme)

#####
# simulate data
set.seed(18112003)
na <- 30
nb <- 30

sigma_a <- 1
sigma_b <- .5
sigma_res <- .33

n <- na*nb

a <- gl(na,1,n)
b <- gl(nb,na,n)
u <- gl(1,1,n)

x <- runif(n, -3, 3)

y_no_noise <- x + sin(2 * x)
y <- 
  x + sin(2 * x) + 
  rnorm(na, sd = sigma_a)[as.integer(a)] + 
  rnorm(nb, sd = sigma_b)[as.integer(b)] + 
  rnorm(n, sd = sigma_res)

#####
# works in the linear model where we know the true parameter
fit <- lme(
  # somehow we found the right values
  y ~ x + sin(2 * x), 
  random = list(u = pdBlocked(list(pdIdent(~ a - 1), pdIdent(~ b - 1)))))
vv <- VarCorr(fit)
vv2 <- vv[c("a1", "b1"), ]
storage.mode(vv2) <- "numeric"
print(vv2,digits=4)
#R    Variance StdDev
#R a1    1.016 1.0082
#R b1    0.221 0.4701

#####
# now try to do the same with `nlme`
fit <- nlme(
  y ~ c0 + sin(c1),
  fixed = list(c0 ~ x, c1 ~ x - 1),
  random = list(u = pdBlocked(list(pdIdent(~ a - 1), pdIdent(~ b - 1)))), 
  start = c(0, 0.5, 1))
#R Error in nlme.formula(y ~ a * x + sin(b * x), fixed = list(a ~ 1, b ~  : 
#R    'random' must be a formula or list of formulae 

The lme example is similar to the one page 163-166 of "Mixed-effects Models in S and S-PLUS" with only 2 random effects instead of 3.

like image 615
Benjamin Christoffersen Avatar asked Aug 03 '18 20:08

Benjamin Christoffersen


People also ask

What is a crossed random effect?

Crossed random effects means that a given factor appears in more than one level of the upper level factor. For example, there are pupils within classes measured over several years.

What is a nested effect?

Nested random effects are when each member of one group is contained entirely within a single unit of another group. The canonical example is students in classrooms; you may have repeated measures per student, but each student belongs to a single classroom (assuming no reassignments).

Can you have multiple random effects?

A model formula can include several such random effects terms. Because configurations such as nested or crossed or partially crossed grouping factors are a property of the data, the specification in the model formula does not depend on the configuration.

What is a two way random effects model?

In a two-way ANOVA with interaction, if both factors have non-random levels, then it is called a fixed effects design. If both factors have levels that are chosen at random, then it is called a random effects design.


Video Answer


1 Answers

I should haved used a two-sided formula as written in help("nlme")

fit <- nlme(
  y ~ c0 + c1 + sin(c2),
  fixed = list(c0 ~ 1, c1 ~ x - 1, c2 ~ x - 1),
  random = list(u = pdBlocked(list(pdIdent(c0 ~ a - 1), pdIdent(c1 ~ b - 1)))), 
  start = c(0, 0.5, 1))

# fixed effects estimates
fixef(fit)
#R c0.(Intercept)           c1.x           c2.x 
#R     -0.1788218      0.9956076      2.0022338

# covariance estimates
vv <- VarCorr(fit)
vv2 <- vv[c("c0.a1", "c1.b1"), ]
storage.mode(vv2) <- "numeric"
print(vv2,digits=4)
#R       Variance StdDev
#R c0.a1   0.9884 0.9942
#R c1.b1   0.2197 0.4688
like image 124
Benjamin Christoffersen Avatar answered Nov 03 '22 12:11

Benjamin Christoffersen