从lme4模型获取预测变量的类别

问题描述 投票:1回答:1

[拟合lme4模型后,我想知道如何从terms(fit)[[3]]中获得预测变量的类?

这是一个简单的示例,但我赞赏lme4中任何其他模型的功能性回答。

注:必须从模型中提取所有内容。

library(lme4)

h <- read.csv('https://raw.githubusercontent.com/hkil/m/master/h.csv')
h$year <- as.factor(h$year)
m <- lmer(scale~ year*group + (1|stid), data = h)

terms(m)[[3]]  ## What are the `class`es of the variables in here (e.g., `integer`, `factor` etc.)
r dataframe class lm lme4
1个回答
0
投票

也许不够完美,但是:

  1. 从条件对象中提取变量的名称
av <- all.vars(terms(m)[[3]])   ## c("year","group")
  1. data=提供的数据框中查找它们:
setNames(lapply(av, function(x) class(h[[x]])), av)
$year
[1] "factor"

$group
[1] "character"

如果要从模型中获取所有内容,通常会得到MUCH HARDER,因为原始变量不一定要存储。在示例中,您进行了此工作:

 setNames(lapply(av, function(x) class(model.frame(m)[[x]])), av)
$year
[1] "factor"

$group
[1] "factor"

您会注意到group已转换为因子。您可以例如通过在模型中使用类似log(x)的术语来打破这一点...

© www.soinside.com 2019 - 2024. All rights reserved.