User Tools

Site Tools


r:multiple_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:multiple_regression [2020/12/01 14:30] – [Multiple Regression] hkimscilr:multiple_regression [2023/10/19 08:23] (current) hkimscil
Line 19: Line 19:
 </code> </code>
 <code> <code>
-three.predictor.model <- lm(ROLL ~ UNEM + HGRAD + INC, datavar) 
-summary(two.predictor.model) 
-two.predictor.model 
- 
 two.predictor.model <- lm(ROLL ~ UNEM + HGRAD, datavar) two.predictor.model <- lm(ROLL ~ UNEM + HGRAD, datavar)
 summary(two.predictor.model) summary(two.predictor.model)
 two.predictor.model two.predictor.model
 +</code>
  
 +<code>
 +three.predictor.model <- lm(ROLL ~ UNEM + HGRAD + INC, datavar)
 +summary(three.predictor.model)
 +three.predictor.model
 </code> </code>
  
-[[:sequential_regression#eg_3_college_enrollment_in_new_mexico_university|Sequential method]]+<code> 
 +> two.predictor.model <- lm(ROLL ~ UNEM + HGRAD, datavar) 
 +> summary(two.predictor.model)
  
 +Call:
 +lm(formula = ROLL ~ UNEM + HGRAD, data = datavar)
 +
 +Residuals:
 +    Min      1Q  Median      3Q     Max 
 +-2102.2  -861.6  -349.4   374.5  3603.5 
 +
 +Coefficients:
 +              Estimate Std. Error t value Pr(>|t|)    
 +(Intercept) -8.256e+03  2.052e+03  -4.023  0.00044 ***
 +UNEM         6.983e+02  2.244e+02   3.111  0.00449 ** 
 +HGRAD        9.423e-01  8.613e-02  10.941 3.16e-11 ***
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 1313 on 26 degrees of freedom
 +Multiple R-squared:  0.8489, Adjusted R-squared:  0.8373 
 +F-statistic: 73.03 on 2 and 26 DF,  p-value: 2.144e-11
 +
 +> two.predictor.model
 +
 +Call:
 +lm(formula = ROLL ~ UNEM + HGRAD, data = datavar)
 +
 +Coefficients:
 +(Intercept)         UNEM        HGRAD  
 + -8255.7511     698.2681       0.9423  
 +
 +
 +</code>
 +
 +<code>
 +> three.predictor.model <- lm(ROLL ~ UNEM + HGRAD + INC, datavar)
 +> summary(three.predictor.model)
 +
 +Call:
 +lm(formula = ROLL ~ UNEM + HGRAD + INC, data = datavar)
 +
 +Residuals:
 +     Min       1Q   Median       3Q      Max 
 +-1148.84  -489.71    -1.88   387.40  1425.75 
 +
 +Coefficients:
 +              Estimate Std. Error t value Pr(>|t|)    
 +(Intercept) -9.153e+03  1.053e+03  -8.691 5.02e-09 ***
 +UNEM         4.501e+02  1.182e+02   3.809 0.000807 ***
 +HGRAD        4.065e-01  7.602e-02   5.347 1.52e-05 ***
 +INC          4.275e+00  4.947e-01   8.642 5.59e-09 ***
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 670.4 on 25 degrees of freedom
 +Multiple R-squared:  0.9621, Adjusted R-squared:  0.9576 
 +F-statistic: 211.5 on 3 and 25 DF,  p-value: < 2.2e-16
 +
 +> three.predictor.model
 +
 +Call:
 +lm(formula = ROLL ~ UNEM + HGRAD + INC, data = datavar)
 +
 +Coefficients:
 +(Intercept)         UNEM        HGRAD          INC  
 + -9153.2545     450.1245       0.4065       4.2749  
 +
 +
 +</code>
 +
 +만약에 
 +  * unemployment rate (UNEM) = 9%, 12%, 3%
 +  * spring high school graduating class (HGRAD) = 100000, 98000, 78000
 +  * a per capita income (INC) of \$30000, \$28000, \$36000
 +  * 일 때, enrollment는 어떻게 predict할 수 있을까?
 +
 +위에서 얻은 prediction model은 아래와 같다. 
 +$$ \hat{Y} = -9153.2545 + 450.1245 \cdot UNEM + 0.4065 \cdot HGRAD + 4.2749 \cdot INC $$
 +여기에 위의 정보를 대입해 보면 된다. 
 +
 +<code>
 +new.data <- data.frame(UNEM=c(9, 12, 3), HGRAD=c(100000, 98000, 78000), INC=c(30000, 28000, 36000))
 +predict(three.predictor.model, newdata=new.data)
 +</code>
 +
 +<code>
 +> new.data <- data.frame(UNEM=c(9, 10, 15), HGRAD=c(100000, 98000, 78000), INC=c(30000, 28000, 19000))
 +> predict(three.predictor.model, newdata=new.data)
 +              2        3 
 +163792.0 154879.4 110526.6 
 +
 +</code>
 +\begin{align*}
 +\hat{Y} & = -9153.2545 + 450.1245 \cdot \text{UNEM} + 0.4065 \cdot \text{HGRAD} + 4.2749 \cdot \text{INC}  \\ 
 +163792.0 & = -9153.2545 + 450.1245 \cdot (9) + 0.4065 \cdot (100000) + 4.2749 \cdot (30000) \\
 +154879.4 & = -9153.2545 + 450.1245 \cdot (10) + 0.4065 \cdot (98000) + 4.2749 \cdot (28000) \\
 +110526.6 & = -9153.2545 + 450.1245 \cdot (15) + 0.4065 \cdot (78000) + 4.2749 \cdot (19000) \\
 +
 +\end{align*}
 +
 +beta coefficient 살펴보기 
 +see [[:beta coefficients]]
 +<code>
 +# install.packages('lm.beta')
 +# library(lm.beta)
 +lm.beta(three.predictor.model)
 +</code>
 +
 +<code>
 +> # install.packages('lm.beta')
 +> # library(lm.beta)
 +> lm.beta(three.predictor.model)
 +
 +Call:
 +lm(formula = ROLL ~ UNEM + HGRAD + INC, data = datavar)
 +
 +Standardized Coefficients::
 +(Intercept)        UNEM       HGRAD         INC 
 +  0.0000000   0.1553619   0.3656177   0.6061762 
 +
 +
 +</code>
 +by hand
 +<code>
 +# coefficient * (sd(x)/sd(y)) 이므로
 +
 +attach(datavar)
 +sd.roll <- sd(ROLL)
 +sd.unem <- sd(UNEM)
 +sd.hgrad <- sd(HGRAD)
 +sd.inc <- sd(INC)
 +
 +b.unem <- three.predictor.model$coefficients[2]
 +b.hgrad <- three.predictor.model$coefficients[3] 
 +b.inc <- three.predictor.model$coefficients[4]
 +
 +## or
 +b.unem <- 4.501e+02
 +b.hgrad <- 4.065e-01
 +b.inc <- 4.275e+00
 +
 +
 +b.unem * (sd.unem / sd.roll)
 +b.hgrad * (sd.hgrad / sd.roll)
 +b.inc * (sd.inc / sd.roll)
 +
 +lm.beta(three.predictor.model)
 +
 +</code>
 +output of the above
 +<code>
 +> sd.roll <- sd(ROLL)
 +> sd.unem <- sd(UNEM)
 +> sd.hgrad <- sd(HGRAD)
 +> sd.inc <- sd(INC)
 +
 +> b.unem <- three.predictor.model$coefficients[2]
 +> b.hgrad <- three.predictor.model$coefficients[3] 
 +> b.inc <- three.predictor.model$coefficients[4]
 +
 +> ## or
 +> b.unem <- 4.501e+02
 +> b.hgrad <- 4.065e-01
 +> b.inc <- 4.275e+00
 +
 +
 +> b.unem * (sd.unem / sd.roll)
 +[1] 0.1554
 +> b.hgrad * (sd.hgrad / sd.roll)
 +[1] 0.3656
 +> b.inc * (sd.inc / sd.roll)
 +[1] 0.6062
 +
 +> lm.beta(three.predictor.model)
 +
 +Call:
 +lm(formula = ROLL ~ UNEM + HGRAD + INC, data = datavar)
 +
 +Standardized Coefficients::
 +(Intercept)        UNEM       HGRAD         INC 
 +     0.0000      0.1554      0.3656      0.6062 
 +
 +
 +</code>
 +
 +see also [[:sequential_regression#eg_3_college_enrollment_in_new_mexico_university|Sequential method]] regression modeling by hand
 +see also [[:statistical regression methods]] regression modeling by computing 
 +
 +<code>
 +> fit <- three.predictor.model
 +> step <- stepAIC(fit, direction="both")
 +Start:  AIC=381.2
 +ROLL ~ UNEM + HGRAD + INC
 +
 +        Df Sum of Sq      RSS AIC
 +<none>               11237313 381
 +- UNEM     6522098 17759411 392
 +- HGRAD  1  12852039 24089352 401
 +- INC    1  33568255 44805568 419
 +
 +
 +</code>
 ====== Housing ====== ====== Housing ======
 {{housing.txt}} {{housing.txt}}
Line 37: Line 239:
  
 ====== etc ====== ====== etc ======
 +{{:marketing_from_datarium.csv}}
 <code> <code>
 +marketing <- read.csv("http://commres.net/wiki/_media/marketing_from_datarium.csv")
 +</code>
 +
 +<code>
 +# install.packages("tidyverse", dep=TRUE)
 library(tidyverse) library(tidyverse)
 data("marketing", package = "datarium") data("marketing", package = "datarium")
Line 45: Line 253:
   * Note that to list all the independent (explanatory) variables, you could use ''lm (sales ~ ., data="marketing")''.   * Note that to list all the independent (explanatory) variables, you could use ''lm (sales ~ ., data="marketing")''.
   * You could also use ''-'' sign to subtract ivs. ''lm(sales ~ . - newspapers, data = "marketing")''   * You could also use ''-'' sign to subtract ivs. ''lm(sales ~ . - newspapers, data = "marketing")''
 +
      
 <code> <code>
Line 271: Line 480:
 | interest  | 1 (a)  | 894463 (1)  | 894463  | 179.6471179  | | interest  | 1 (a)  | 894463 (1)  | 894463  | 179.6471179  |
 | unemp  | 1 (b)  | 22394 (2) | 22394  | 4.497690299  | | unemp  | 1 (b)  | 22394 (2) | 22394  | 4.497690299  |
-| res  | 21 (c)  | 104559 (3) | 4979  |   |+| res  | 21 %%(%%c%%)%%  | 104559 (3) | 4979  |   |
 | total  | 23  | 1021416 (4)  |     | | total  | 23  | 1021416 (4)  |     |
-interst \\ + enemp  |   | 916857 (5)  |     |+interest \\ + enemp  |   | 916857 (5)  |     |
  
 (4) = (1) + (2) + (3) (4) = (1) + (2) + (3)
r/multiple_regression.1606800633.txt.gz · Last modified: 2020/12/01 14:30 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki