R 用于汇集已安装型号的回路

R 用于汇集已安装型号的回路,r,for-loop,R,For Loop,我是R方面的初学者,所以想寻求一些帮助 我正在尝试使用for循环来迭代我的插补拟合模型,以便在合并模型和随后计算模型的Rsquared时提高一些效率 # Model with all Trust variables fits_mod1 <- lm.mids(Trust ~ Tr_Family + Tr_Neighborhood + Tr_Personally + Tr_Initial + Tr_Nationality, data = miceO

我是R方面的初学者,所以想寻求一些帮助

我正在尝试使用for循环来迭代我的插补拟合模型,以便在合并模型和随后计算模型的Rsquared时提高一些效率

# Model with all Trust variables

fits_mod1 <- lm.mids(Trust ~  Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality, data = miceOut3)

# Model with all Trust + Discriminatory attitudes variables

fits_mod2 <- lm.mids(Trust ~  Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Racism_neighborhood + Homosexuality, data = miceOut3)

# Model with all Trust + Police variables

fits_mod3 <- lm.mids(Trust ~  Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality 
                     + Confidence_police + Interfere_police, data = miceOut3)

# Model with all Trust + Happiness variables

fits_mod4 <- lm.mids(Trust ~ Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Satisfaction + Feeling_happy, data = miceOut3)

# Model with all Trust + Danger variables

fits_mod5 <- lm.mids(Trust ~ Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Violence + Avoid_danger, data = miceOut3)

# Model with all Trust + Control and Advantage variables

fits_mod6 <- lm.mids(Trust ~ Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Adv_Taken + Control_life 
                     + Wealth_accumulation, data = miceOut3)

## Pool the fitted models:
poolFit1 <- pool(fits_mod1)
poolFit2 <- pool(fits_mod2)
poolFit3 <- pool(fits_mod3)
poolFit4 <- pool(fits_mod4)
poolFit5 <- pool(fits_mod5)
poolFit6 <- pool(fits_mod6)

## Compute the pooled R^2:
pool.r.squared(fits_mod1)
pool.r.squared(fits_mod2)
pool.r.squared(fits_mod3)
pool.r.squared(fits_mod4)
pool.r.squared(fits_mod5)
pool.r.squared(fits_mod6)

# select the model with highest rsquared 
pool.r.squared(fits_mod2)[1] - pool.r.squared(fits_mod1)[1]
#具有所有信任变量的模型

fits_mod1我想你是在找
ls()
然后
get
。假设您的工作区中已经安装了模型,称为
fits\u mod1
fits\u mod2
fits\u mod3

fits_mods <- ls(pattern="^fits_mod\\d+")
fits_mods
# [1] "fits_mod1" "fits_mod2" "fits_mod3"

get(fits_mod[1]) # This shows the results.

poolFits <- list()
for(i in 1:3) {
  poolFits[[i]] <- pool(get(fits_mod[i]))
}

poolFits # show them all

# select the model with highest r-squared 
pool.r.squared(get(fits_mods[2]))[1] - pool.r.squared(get(fits_mods[1]))[1]

fits_mods一种更像R的方法是这样的。将您的不同公式存储在列表中,然后在列表上使用
lappy
来拟合和总结您的模型

models <- list(
  Trust ~  Tr_Family + Tr_Neighborhood + Tr_Personally 
                     + Tr_Initial + Tr_Nationality,
  Trust ~  Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Racism_neighborhood + Homosexuality,
  Trust ~  Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality 
                     + Confidence_police + Interfere_police,
  Trust ~ Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Satisfaction + Feeling_happy,
  Trust ~ Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Violence + Avoid_danger,
  Trust ~ Tr_Family + Tr_Neighborhood + Tr_Personally
                     + Tr_Initial + Tr_Nationality
                     + Adv_Taken + Control_life 
                     + Wealth_accumulation)

fits <- lapply(models, lm.mids, data=miceOut3)
pools <- lapply(fits, pool)
poolR2 <- lapply(fits, pool.r.squared)

模型首先,您是如何创建所有这些
拟合对象的?有一堆全局变量的名称中有索引,这表明您可能没有以最类似R的方式进行操作。最好将相关数据保存在列表中。然后你就可以很容易地在这个列表上应用一个函数,而不需要显式循环。嗨,Flick先生,我添加了生成fits_mod对象的代码。正如你所提到的,我是以一种“Python”的心态来处理它的,这对R来说真的不起作用。谢谢你的回复。嗨,爱德华。这正是我想要的。我是以不同的思维方式处理这个问题的,但是您的回答确实帮助我澄清了如何在R中处理for循环和多个对象。非常感谢!!当我尝试使用您提到的代码时,它返回:'>fits,我有一个错误,现在已经修复了。我忘了将
lm.mids
函数传递给
lappy
。非常感谢!我尝试将函数添加到它,但忘记删除
lappy
lm.mids()
中的()。非常感谢您抽出时间!