User Tools

Site Tools


r:linear_regression

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
r:linear_regression [2018/12/04 03:16] – [Simple Linear Regression] hkimscilr:linear_regression [2019/06/13 10:15] (current) hkimscil
Line 147: Line 147:
 </WRAP> </WRAP>
  
 +<WRAP info>
 +What about beta coefficient?
 +
 +<blockquote>In R we demonstrate the use of the lm.beta() function in the QuantPsyc package (due to Thomas D. Fletcher of State Farm). The function is short and sweet, and takes a linear model object as argument:</blockquote>
 +
 +<code>> lm.beta(mod)
 +EngineSize 
 +-0.7100032 
 +> cor(MPG.city,EngineSize)
 +[1] -0.7100032
 +
 +</code></WRAP>
  
  
 ====== Multiple Regression ====== ====== Multiple Regression ======
 +regression output table
 | anova(m)  | ANOVA table  | | anova(m)  | ANOVA table  |
 | coefficients(m) = coef(m)  | Model coefficients  | | coefficients(m) = coef(m)  | Model coefficients  |
Line 185: Line 198:
 F-statistic: 54.83 on 2 and 90 DF,  p-value: 2.674e-16 F-statistic: 54.83 on 2 and 90 DF,  p-value: 2.674e-16
 </code> </code>
-Questions: +<WRAP box help>Questions: 
   * What is R<sup>2</sup>?   * What is R<sup>2</sup>?
   * How many cars are involved in this test? (cf. df = 90)   * How many cars are involved in this test? (cf. df = 90)
 +    * df + # of variables involved (3) = 93
 +    * check 'str(Cars93)'
   * If I eliminate the R<sup>2</sup> from the above output, can you still identify what it is?   * If I eliminate the R<sup>2</sup> from the above output, can you still identify what it is?
 +</WRAP>
 +<WRAP box info>The last question: 
 +  * If I eliminate the R<sup>2</sup> from the above output, can you still identify what it is?
 +  * to answer the question, use the regression output table:
 +
 +R<sup>2</sup> = SS<sub>reg</sub>/SS<sub>total</sub> = 1 - SS<sub>res</sub>/SS<sub>total</sub>
 +
 +
 +<code>> anova(lm.model)
 +Analysis of Variance Table
 +
 +Response: Cars93$MPG.city
 +                  Df Sum Sq Mean Sq F value  Pr(>F)    
 +Cars93$EngineSize  1   1465    1465  100.65 2.4e-16 ***
 +Cars93$Price          131     131    9.01  0.0035 ** 
 +Residuals         90   1310      15                    
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +> sstotal = 1465+131+1310
 +> ssreg <- 1465+131
 +> ssreg/sstotal
 +[1] 0.54921
 +
 +> # or
 +> 1-(deviance(lm.model)/sstotal)
 +[1] 0.54932
 +</code>
 +
 +
 +</WRAP>
  
 Regression formula: Regression formula:
   * $\hat{Y} = -2.99 * EngineSize + -0.15 * Price +  33.35$   * $\hat{Y} = -2.99 * EngineSize + -0.15 * Price +  33.35$
   * $\hat{Y} = \widehat{\text{MPG.city}}$   * $\hat{Y} = \widehat{\text{MPG.city}}$
 +
 +<WRAP box info>in the meantime,
 +<code>> lm.beta(lm.model)
 +Cars93$EngineSize      Cars93$Price 
 +       -0.5517121        -0.2649553 
 +> cor(MPG.city,EngineSize)
 +[1] -0.7100032
 +> cor(EngineSize,Price)
 +[1] 0.5974254
 +> cor(MPG.city,Price)
 +[1] -0.5945622
 +
 +</code>
 +Or . . . . 
 +<code>> temp <- subset(Cars93, select=c(MPG.city,EngineSize,Price))
 +> temp
 +. . . . 
 +
 +> cor(temp)
 +             MPG.city EngineSize      Price
 +MPG.city    1.0000000 -0.7100032 -0.5945622
 +EngineSize -0.7100032  1.0000000  0.5974254
 +Price      -0.5945622  0.5974254  1.0000000
 +
 +</code>
 +Beta coefficients are not equal to correlations among variables. 
 +</WRAP>
  
 <code>plot(lm.model$residuals) <code>plot(lm.model$residuals)
r/linear_regression.1543860980.txt.gz · Last modified: 2018/12/04 03:16 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki