如何使用 Amelia 的插补数据创建一个函数来运行多个回归模型?

问题描述

我有一个多重插补数据集(使用 Amelia 插补),并且想推导出一个函数,该函数可以一次性运行多个回归(具有相同的结果和不同的预测变量)。原因是我需要对不同的结果变量运行多个回归(使用相同的预测变量)。

我已经定义了函数,但是当我运行它时,我收到一条错误消息:

model.frame.default(formula = 结果 ~ country,data = as.data.frame(.),中的错误: 可变长度不同(找到“国家”)

我认为这是因为估算的数据集是一个“amelia”对象而不是一个典型的数据框,所以很难索引变量“country”。但我不知道该怎么做。

下面是一个可重现的示例。我真的很感激任何建议。

library(Amelia)
library(Zelig)

# Use africa dataset
data(africa)
# Impute data
imp.out <- amelia(x = africa,cs = "country",ts = "year",logs = "gdp_pc",m=5)
summary(imp.out)

# Define function to run regression predicting an outcome from country,gdp_pc,civlib,and population
reg_function <- function(outcome,data) {
  
  # Run regressions using the zelig function
  country <- zelig(outcome ~ country,model = "normal",data=data,cite=FALSE)
  gdp_pc <- zelig(outcome ~ gdp_pc,cite=FALSE)
  population <- zelig(outcome ~ population,cite=FALSE)
  
  # Put results into a vector 
  results <- ( c(combine_coef_se(country)[2,1],combine_coef_se(country)[2,2],combine_coef_se(gdp_pc)[2,combine_coef_se(population)[2,2]))
  
  # Return results in a matrix
  return(matrix(results,nrow=1,ncol=6,dimnames=list(c(""),c("Est_country","SE_country","Est_gdp_pc","SE_gdp_pc","Est_population","SE_population"))))
  
}
  
# Run regression for outcome variables "year","infl",and "Trade" 
# This is where I get the error messages that the variable lengths differ for 'country'
year <- reg_function(year,imp.out$imputations)
year <- reg_function(infl,imp.out$imputations)
year <- reg_function(Trade,imp.out$imputations)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)