【问题标题】:Linear models in R with different combinations of data frame variablesR中具有不同数据框变量组合的线性模型
【发布时间】:2019-05-28 09:19:30
【问题描述】:

我有一个数据框 (myvar),其中从 act1_1 到 act1_144(dependent variable) 的列名填充了数值和 7 列,其中包括社会人口统计学、DVAge、DVHsize、dhhtype、deconact、Income、NumChild 和 Rooms (independent variables)。

independents<-DVType[, 1:144] 
dependents<-DVType[, 145:151]



myvar<-cbind(dependents,independents)

我正在尝试使用社会人口统计列变量生成线性回归模型,并尝试所有可能的组合,例如收入、收入+数字、收入+房间、...、dhhtype+deconact ...。我有使用数据框生成组合时遇到问题。

我设法做的是将因变量回归到自变量上。

 fit<-lm(as.matrix(dependents) ~ -1 + model.matrix(~ ., data = independents  ))
    require(broom)
    summary(fit)

Output:

    Response DVHsize :

    Call:
    lm(formula = DVHsize ~ -1 + model.matrix(~., data = independents))

    Residuals:
        Min      1Q  Median      3Q     Max 
    -3.1356 -1.0056 -0.2886  0.9597  7.2341 

    Coefficients:
                                                       Estimate Std. Error t value Pr(>|t|)    
    model.matrix(~., data = independents)(Intercept)  3.616e+00  4.300e-02  84.096  < 2e-16 ***
    model.matrix(~., data = independents)act1_1      -2.788e-05  2.911e-05  -0.958  0.33822    
    model.matrix(~., data = independents)act1_2       3.703e-05  2.898e-05   1.278  0.20138    
    model.matrix(~., data = independents)act1_3      -4.458e-06  2.177e-05  -0.205  0.83773    
    model.matrix(~., data = independents)act1_4       2.120e-05  2.557e-05   0.829  0.40705    
    model.matrix(~., data = independents)act1_5       2.327e-05  2.724e-05   0.854  0.39296    
    model.matrix(~., data = independents)act1_6      -4.578e-05  2.299e-05  -1.991  0.04644 *  
    model.matrix(~., data = independents)act1_7       2.087e-05  1.971e-05   1.058  0.28985    
    model.matrix(~., data = independents)act1_8      -4.694e-06  2.019e-05  -0.233  0.81612    
    model.matrix(~., data = independents)act1_9       3.604e-06  1.756e-05   0.205  0.83738    
    model.matrix(~., data = independents)act1_10     -2.924e-06  1.685e-05  -0.174  0.86225    
    model.matrix(~., data = independents)act1_11      4.934e-06  1.671e-05   0.295  0.76782    

....

如何扩展它以识别所有组合?

【问题讨论】:

    标签: r dataframe regression


    【解决方案1】:

    如果我理解正确,你的公式是错误的, 您的预测变量 (independents) 应该是您提到的 7 列。 我不确定“所有可能的组合”是否正是您想要的, 也许您只想要二阶交互(并且由于-1 而没有拦截)? 在这种情况下,您可能可以执行以下操作 (另见this question):

    fit <- lm(sprintf("cbind(%s) ~ . ^ 2 - 1",
                      toString(paste("act1", 1:144, sep = "_"))),
              data = DVType)
    

    【讨论】:

      猜你喜欢
      • 2014-05-22
      • 2020-12-21
      • 2015-07-21
      • 2017-03-04
      • 2021-02-22
      • 1970-01-01
      • 2021-08-21
      • 2021-08-09
      • 1970-01-01
      相关资源
      最近更新 更多