r - `nlme` 具有交叉随机效应

标签 r nlme random-effects

我正在尝试将交叉非线性随机效应模型拟合为前面提到的线性随机效应模型 in this question在这个 mailing list post使用 nlme 包。不过,无论我尝试什么,我都会收到错误。这是一个例子

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 

lme 示例类似于“S 和 S-PLUS 中的混​​合效应模型”第 163-166 页,只有 2 个随机效应,而不是 3 个。

最佳答案

我应该使用 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

关于r - `nlme` 具有交叉随机效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51679728/

相关文章:

r - 从两个不同的数据帧中获取公共(public)行的索引

r - 聚合函数在数据框中创建不需要的向量

r - 以数据帧 + r + lme 的形式访问 Intervals.lme 的结果

r - 回归模型如何处理因子变量?

r - 具有标准误差的个体随机效应模型聚集在 R 中的不同变量上(R 项目)

r - 从 gamlss 对象中提取随机效应

jags - JAGS/WinBUGS 中的嵌套随机效果

r - 使用 system() 命令在 R 中执行 SAS 程序

r - R 的 powerlaw 包中连续与离散对数正态分布的似然函数差异

r - 混合建模 - lme 和 lmer 函数之间的不同结果