在R软件中拟合多元线性回归模型

时间:2017-10-28 16:44:47

标签: r

我试图在R中使用多元线性回归模型,但我在程序中不断收到此错误消息:

 # Fit a multiple linear regression model
> mlr <- lm(y~x1+x2+x3+x4+x5,data=steam) 
Warning messages:
1: In model.response(mf, "numeric") :
  using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
> summary(mlr)

Call:
lm(formula = y ~ x1 + x2 + x3 + x4 + x5, data = steam)

Residuals:
Error in quantile.default(resid) : factors are not allowed
In addition: Warning message:
In Ops.factor(r, 2) : ‘^’ not meaningful for factors
> names(mlr)
 [1] "coefficients"  "residuals"     "effects"       "rank"         
 [5] "fitted.values" "assign"        "qr"            "df.residual"  
 [9] "xlevels"       "call"          "terms"         "model"        
> mlrs <- summary(mlr)
Warning message:
In Ops.factor(r, 2) : ‘^’ not meaningful for factors
> names(mlrs)
 [1] "call"          "terms"         "residuals"     "coefficients" 
 [5] "aliased"       "sigma"         "df"            "r.squared"    
 [9] "adj.r.squared" "fstatistic"    "cov.unscaled" 

这是我到目前为止的代码:

 >str(steam)
'data.frame':   25 obs. of  6 variables:
 $ y : Factor w/ 25 levels "10","11","12",..: 25 11 18 19 20 21 22 23 24 1 ...
 $ x1: num  10.98 11.13 12.51 8.4 9.27 ...
 $ x2: num  5.2 5.12 6.19 3.89 6.28 5.76 3.45 6.57 5.69 6.14 ...
 $ x3: num  0.61 0.64 0.78 0.49 0.84 0.74 0.42 0.87 0.75 0.76 ...
 $ x4: num  7.4 8 7.4 7.5 5.5 8.9 4.1 4.1 4.1 4.5 ...
 $ x5: int  31 29 31 30 31 30 31 31 30 31 ...

运行str(steam)显示此

try {
    br = new BufferedReader(new FileReader("src/files/names.csv"));
} catch (FileNotFoundException e) {
    throw new RuntimeException(e); 
}

我想知道导致显示警告信息的原因。谢谢!

1 个答案:

答案 0 :(得分:0)

因变量不应是因数,字符或类别。因此,以下代码将是最好的。

# Fit a multiple linear regression model
mlr <- lm(as.numeric(y)~x1+x2+x3+x4+x5,data=steam)