r - 如何获得随机效应模型矩阵?

标签 r statistics regression lme4 mixed-models

我有一个模型,例如:

mymod = lmer(y ~ x1 + x2 + (x1 | id) , data = mydata)

我知道我可以使用getME从拟合对象中获取模型矩阵,但是有没有办法获取固定效应的模型矩阵而无需先拟合模型 :

最佳答案

您可以使用 lme4 包中的 lformula 函数来完成此操作。这将重新生成一个对象,该对象保存了该矩阵的转置,Zt:

library(lme4)

# create some toy data
dt <- expand.grid(x1 = 1:4, x2 = 5:6, id = LETTERS[1:20], reps = 1:2)

# this is the model in the OP:
myFormula = "y ~ x1 + x2 + (x1 | id)"

# for lFormula to work we need y in the data frame
# so just put a vector of 1s since that will not affect the random effects model matrix:
dt$y <- 1

然后:

foo <- lFormula(eval(myFormula), dt)
Z <- t(as.matrix(foo$reTrms$Zt))

其中 Z 是您请求的随机效应的模型矩阵。

关于r - 如何获得随机效应模型矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70709438/

相关文章:

r - plotly:x 轴(日期)上的范围 slider ,具有自定义开始/结束日期

r - 创建、命名、存储和汇总模型的编程方式

python - 自举二项式分布

python - 属性错误: module ‘xgboost’ has no attribute ‘XGBRegressor’

r - 在 R 中使用 "~ call"和动态变量

r - 过滤掉 data.table 中的重复/非唯一行

r - R function() 中的美元符号 "$"是什么意思?

r - lavaan 中具有多个中介变量和多个自变量的 SEM 模型

php - 在图像上找到最喜欢的区域

python - scikit-learn 中的随机森林解释