简体   繁体   中英

Fitting multiple linear regression model in R software

I was attempting to fit a multiple linear regression model in R but I kept getting this error message in my program:

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

Here's the code I have so far:

 # 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" 

The running str(steam) shows this

 >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 ...

I would like to know what has caused the warning message shown. Thank you!

Dependent variable should not be factor, character or categorical. So, the following code would be best.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM