[英]Create list of dataframes displaying AICc values from a nested list of models
I have a list of statistical models: 我有一个统计模型列表:
###Data import
Responses <- as.data.frame(matrix(sample(0:10, 1*100, replace=TRUE), ncol=2))
colnames(Responses) <- c("A","B")
Explanatories <- as.data.frame(matrix(sample(20:30, 1*100, replace=TRUE), ncol=2))
colnames(Explanatories) <- c("x","y")
###Create models
Models <- list(
lm(Responses$A ~ Explanatories$x),
lm(Responses$B ~ log10(Explanatories$x)),
lm(Responses$B ~ exp(Explanatories$y))
)
And a list of null models. 以及空模型列表。 These correspond to the models in the previous list, so for example I would want to compare the first lm in 'Models' against the first lm in 'Models_null':
这些对应于上一个列表中的模型,因此例如,我想将“ Models”中的第一个lm与“ Models_null”中的第一个lm进行比较:
Models_null <- list(
lm(Responses$A ~ 1),
lm(Responses$B ~ 1),
lm(Responses$B ~ 1)
)
All_models <- list(Models,Models_null)
names(All_models)<-c("full","res")
I would like to calculate an AICc score for each model using this formula: 我想使用以下公式为每个模型计算AICc分数:
aicc<-function(x) AIC(x)+((2*length(coef(x))*(length(coef(x))+1))/(length(resid(x))-length(coef(x))-1))
I need to do some calculations based on how AICc scores compare between each respective full and null model. 我需要根据每个完整和空模型之间的AICc分数进行比较来进行一些计算。 So what I would need is a list of dataframes displaying the AICc scores for each pair of models, with 2 columns for 'full' and 'null .
因此,我需要的是一个数据帧列表,其中显示了每对模型的AICc得分,其中两列分别为“ full”和“ null” 。 Thank you in advance for any help with this.
在此先感谢您的帮助。
Here's a double sapply
: 这是一个双重
sapply
:
sapply(All_models, sapply, aicc)
# full res
# [1,] 267.4959 266.0534
# [2,] 251.9809 251.9127
# [3,] 253.5760 251.9127
The first sapply
only goes over Models
and Models_null
. 第一个
sapply
仅遍历Models
和Models_null
。 Then naturally for each of those two lists we again want to apply sapply
, where for each model we will use aicc
. 然后自然要针对这两个列表中的每一个再次应用
sapply
,对于每种模型,我们将在其中使用aicc
。 That's precisely the @Rui Barradas' solution just in one line. 这恰好就是@Rui Barradas的解决方案。
If needed, afterwards the result may be converted to a data frame, of course. 当然,如果需要,随后可以将结果转换为数据帧。
As to instead get a list whose elements are the rows of the above matrix, any of the following three will do: 为了获得一个列表,该列表的元素为上述矩阵的行,可以使用以下三个方法之一:
lapply(seq_along(Models), function(i)
data.frame(full = aicc(Models[[i]]), null = aicc(Models_null[[i]])))
Map(data.frame, full = lapply(Models, aicc), null = lapply(Models_null, aicc))
do.call(Map, c(data.frame, lapply(All_models, sapply, aicc)))
This is just an application of *apply
functions. 这只是
*apply
函数的一个应用。 I have chosen sapply
because it returns a vector if possible. 我选择
sapply
是因为它尽可能返回一个向量。
Full <- sapply(Models, aicc)
Null <- sapply(Models_null, aicc)
df_aicc <- data.frame(Full, Null)
df_aicc
# Full Null
#1 269.7858 268.5274
#2 254.6533 253.0980
#3 254.8408 253.0980
In order to split this dataframe into a list of df's with one row per df use, well, split
. 为了将此数据帧拆分为df的列表,每个df使用一行,好,
split
。 By row name, which in R must be unique. 按行名,它在R中必须是唯一的。
list_aicc <- split(df_aicc, row.names(df_aicc))
list_aicc
#$`1`
# Full Null
#1 269.7858 268.5274
#
#$`2`
# Full Null
#2 254.6533 253.098
#
#$`3`
# Full Null
#3 254.8408 253.098
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.