用混杂变量拟合R中的线性回归模型

时间:2018-06-19 11:20:21

标签: r regression categorical-data

我有一个名为datamoth的数据集,其中survival是响应变量,而treatment是既可以分类又可以量化的变量。数据集如下所示:

survival <- c(17,22,26,20,11,14,37,26,24,11,11,16,8,5,12,3,5,4,14,8,4,6,3,3,10,13,5,7,3,3)
treatment <- c(3,3,3,3,3,3,6,6,6,6,6,6,9,9,9,9,9,9,12,12,12,12,12,12,21,21,21,21,21,21)
days <- c(3,3,3,3,3,3,6,6,6,6,6,6,9,9,9,9,9,9,12,12,12,12,12,12,21,21,21,21,21,21)

datamoth <- data.frame(survival, treatment)

因此,我可以将线性处理考虑为分类来拟合线性回归模型,如下所示:

lmod<-lm(survival ~ factor(treatment), datamoth)

我的问题是如何将治疗作为分类变量,同时将治疗作为定量混杂变量,以拟合线性回归模型。 我发现了这样的东西:

model <- lm(survival ~ factor(treatment) + factor(treatment)*days, data = datamoth)
summary(model)

Call:
lm(formula = survival ~ factor(treatment) + factor(treatment) * 
    days, data = datamoth)

Residuals:
   Min     1Q Median     3Q    Max 
-9.833 -3.333 -1.167  3.167 16.167 

Coefficients: (5 not defined because of singularities)
                         Estimate Std. Error t value Pr(>|t|)    
(Intercept)                18.333      2.435   7.530 6.96e-08 ***
factor(treatment)6          2.500      3.443   0.726  0.47454    
factor(treatment)9        -12.167      3.443  -3.534  0.00162 ** 
factor(treatment)12       -12.000      3.443  -3.485  0.00183 ** 
factor(treatment)21       -11.500      3.443  -3.340  0.00263 ** 
days                           NA         NA      NA       NA    
factor(treatment)6:days        NA         NA      NA       NA    
factor(treatment)9:days        NA         NA      NA       NA    
factor(treatment)12:days       NA         NA      NA       NA    
factor(treatment)21:days       NA         NA      NA       NA    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 5.964 on 25 degrees of freedom
Multiple R-squared:  0.5869,    Adjusted R-squared:  0.5208 
F-statistic: 8.879 on 4 and 25 DF,  p-value: 0.0001324

但是很明显,此代码无法正常工作,因为这两个变量是共线的。
有谁知道如何解决它?任何帮助将不胜感激。

0 个答案:

没有答案