Samoth
Samoth

Reputation: 756

R- How to save the console data into a row/matrix or data frame for future use?

I'm performing the multiple regression to find the best model to predict the prices. See as following for the output in the R console.

I'd like to store the first column (Estimates) into a row/matrix or data frame for future use such as using R shiny to deploy on the web.

*(Price = 698.8+0.116*voltage-70.72*VendorCHICONY -36.6*VendorDELTA-66.8*VendorLITEON-14.86*H)*

Can somebody kindly advise?? Thanks in advance.

  Call:
  lm(formula = Price ~ Voltage + Vendor + H, data = PSU2)

  Residuals:
  Min       1Q   Median       3Q      Max 
  -10.9950  -0.6251   0.0000   3.0134  11.0360 

  Coefficients:
                   Estimate Std. Error t value Pr(>|t|)    
  (Intercept)   698.821309 276.240098   2.530   0.0280 *  
  Voltage         0.116958   0.005126  22.818 1.29e-10 ***
  VendorCHICONY -70.721088   9.308563  -7.597 1.06e-05 ***
  VendorDELTA   -36.639685   5.866688  -6.245 6.30e-05 ***
  VendorLITEON  -66.796531   6.120925 -10.913 3.07e-07 ***
  H             -14.869478   6.897259  -2.156   0.0541 .  
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

  Residual standard error: 7.307 on 11 degrees of freedom
  Multiple R-squared:  0.9861,  Adjusted R-squared:  0.9799 
  F-statistic: 156.6 on 5 and 11 DF,  p-value: 7.766e-10

Upvotes: 0

Views: 112

Answers (2)

Samoth
Samoth

Reputation: 756

Besides, I'd like to know if there is command to show the "residual percentage" =(actual value-fitted value)/actual value"; currently the "residuals()" command can only show the below info but I need the percentage instead.

residuals(fit3ab)
 1             2             3             4             5             6      
-5.625491e-01 -5.625491e-01  7.676578e-15 -8.293815e+00 -5.646900e+00  3.443652e+00 

Upvotes: 0

mathematical.coffee
mathematical.coffee

Reputation: 56915

Use coef on your lm output.

e.g.

m <- lm(Sepal.Length ~ Sepal.Width + Species, iris)
summary(m)

# Call:
# lm(formula = Sepal.Length ~ Sepal.Width + Species, data = iris)

# Residuals:
#      Min       1Q   Median       3Q      Max 
# -1.30711 -0.25713 -0.05325  0.19542  1.41253 
# 
# Coefficients:
#                   Estimate Std. Error t value Pr(>|t|)    
# (Intercept)         2.2514     0.3698   6.089 9.57e-09 ***
# Sepal.Width         0.8036     0.1063   7.557 4.19e-12 ***
# Speciesversicolor   1.4587     0.1121  13.012  < 2e-16 ***
# Speciesvirginica    1.9468     0.1000  19.465  < 2e-16 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.438 on 146 degrees of freedom
# Multiple R-squared:  0.7259,  Adjusted R-squared:  0.7203 
# F-statistic: 128.9 on 3 and 146 DF,  p-value: < 2.2e-16

coef(m)
#       (Intercept)       Sepal.Width Speciesversicolor  Speciesvirginica 
#         2.2513932         0.8035609         1.4587431         1.9468166 

See also names(m) which shows you some things you can extract, e.g. m$residuals (or equivalently, resid(m)).

And also methods(class='lm') will show you some other functions that work on a lm.

> methods(class='lm')
 [1] add1           alias          anova          case.names     coerce         confint        cooks.distance deviance       dfbeta         dfbetas        drop1          dummy.coef     effects        extractAIC     family        
[16] formula        hatvalues      influence      initialize     kappa          labels         logLik         model.frame    model.matrix   nobs           plot           predict        print          proj           qr            
[31] residuals      rstandard      rstudent       show           simulate       slotsFromS3    summary        variable.names vcov    

(oddly, 'coef' is not in there? ah well)

Upvotes: 2

Related Questions