我一直在使用此处解释的代码:如何以 LaTex 格式在 R 中创建 AIC 模型选择表?
但是,我最近的数据集遇到了错误,我认为这与拥有大量模型项有关(我有 11 列带有模型项)
下面是我的代码:
library(MuMIn)
out.put<-model.sel(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, m16, m17, m18, m19, m20, m21, m22, m23, m24,
m25, m26, m27, m28, m29, m30, m31, m32, m33, m34, m35, m36, m37, m38, m39, m40, m41, m42, m43, m44, m45,
m46, m47, m48, m49, m50, m51, m52, m53, m54, m55, m56, m57, m58, m59, m60, m61, m62, m63, m64, m65, m66,
m67, m68, m69, m70, m71, m72, m73, m74, m75, m76, m77, m78, m79, m80, m81, m82, m83, m84, m85, m86, m87,
m88, m89, m90, m91, m92, m93, m94, m95, m96, m97, m98, m99, m100, m101, m102, m103, m104, m105, m106, m107, m108,
m109, m110, m111, m112, mnull)
i <- 1:11 # indices of columns with model terms
response <- "Survival"
res <- as.data.frame(out.put)
v <- names(out.put)[i]
v[v == "(Intercept)"] <- 1
# create formula-like model names:
mnames <- apply(res[, i], 1, function(x)
deparse(simplify.formula(reformulate(v[!is.na(x)], response = response))))
res <- cbind(model = mnames, res[, -i])
一切看起来都很好,直到我查看一些模型看起来像这样的“mnames”:
$m19
[1]《生存~Inj*年+Trt*年+Trt*性别+Inj*Trt+》《Inj*性别+年*性别》
当我运行最后一行代码时出现此错误: Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 2, 113
我认为问题出在 mnames 中的“”上,但我不知道为什么会这样。我试图缩短名称以查看是否是由于大小造成的,但这似乎不起作用(尽管我可能没有将所有内容都缩短到足够的程度)。我还尝试删除'simplify.formula',以防万一,但仍然是同样的问题。关于如何解决这个问题的任何想法?
谢谢
回应 SamR 的评论
> sapply(out.put, class)
$`(Intercept)`
[1] "numeric"
$Inject
[1] "factor"
$Trt
[1] "factor"
$Year
[1] "factor"
$`Inject:Year`
[1] "factor"
$`Trt:Year`
[1] "factor"
$Sex
[1] "factor"
$`Sex:Trt`
[1] "factor"
$`Inject:Trt`
[1] "factor"
$`Inject:Sex`
[1] "factor"
$`Sex:Year`
[1] "factor"
$family
[1] "character"
$df
[1] "integer"
$logLik
[1] "numeric"
$AICc
[1] "numeric"
$delta
[1] "numeric"
$weight
[1] "model.weights" "numeric"
> sapply(out.put, dim)
$`(Intercept)`
NULL
$Inject
NULL
$Trt
NULL
$Year
NULL
$`Inject:Year`
NULL
$`Trt:Year`
NULL
$Sex
NULL
$`Sex:Trt`
NULL
$`Inject:Trt`
NULL
$`Inject:Sex`
NULL
$`Sex:Year`
NULL
$family
NULL
$df
NULL
$logLik
NULL
$AICc
NULL
$delta
NULL
$weight
NULL