User Tools

Site Tools


multiple_regression_exercise

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
Next revisionBoth sides next revision
multiple_regression_exercise [2021/11/25 20:09] – [Discussion 3] hkimscilmultiple_regression_exercise [2022/11/02 23:41] – [예] hkimscil
Line 151: Line 151:
  
 <code> <code>
 +# install.packages("ISLR")
 +library(ISLR)
 +
 head(Carseats) head(Carseats)
 str(Carseats) str(Carseats)
Line 389: Line 392:
 아래는 설명을 하지 않고 코드만 적었었는데 이는 아래 둘의 model이 동일한 것임을 보여주기 위한 것입니다. 아래는 설명을 하지 않고 코드만 적었었는데 이는 아래 둘의 model이 동일한 것임을 보여주기 위한 것입니다.
 <code> <code>
-lm.2 <- lm(Sales ~ Advertising:ShelveLoc, data = Carseats) ------- (1)   +lm.2 <- lm(Sales ~ Advertising:ShelveLoc, data = Carseats) ------- (1)   
-lm.1 <- lm(Sales ~ Advertising + Advertising:ShelveLoc, data = Carseats) ------- (2)  +lm.1 <- lm(Sales ~ Advertising + Advertising:ShelveLoc, data = Carseats) ------- (2)  
 </code> </code>
  
Line 469: Line 472:
 Coefficients: Coefficients:
                             Estimate Std. Error t value Pr(>|t|)                                 Estimate Std. Error t value Pr(>|t|)    
-(Intercept)                  5.01234    0.31932  15.697  < 2e-16 ***       (1) +(Intercept)                  5.01234    0.31932  15.697  < 2e-16 ***    # (1) 
-ShelveLocGood                4.43573    0.48146   9.213  < 2e-16 ***       (2) +ShelveLocGood                4.43573    0.48146   9.213  < 2e-16 ***    # (2) 
-ShelveLocMedium              1.59511    0.38378   4.156 3.97e-05 ***       (3) +ShelveLocMedium              1.59511    0.38378   4.156 3.97e-05 ***    # (3) 
-ShelveLocBad:Advertising     0.08210    0.03570   2.300  0.02198 *         (4) +ShelveLocBad:Advertising     0.08210    0.03570   2.300  0.02198 *      # (4) 
-ShelveLocGood:Advertising    0.10417    0.03607   2.888  0.00409 **        (5) +ShelveLocGood:Advertising    0.10417    0.03607   2.888  0.00409 **     # (5) 
-ShelveLocMedium:Advertising  0.10692    0.02280   4.689 3.78e-06 ***       (6)+ShelveLocMedium:Advertising  0.10692    0.02280   4.689 3.78e-06 ***    # (6)
 --- ---
 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Line 519: Line 522:
  
 그러니 이렇게 보아도 마찬가지임을 알 수 있죠? 그러니 이렇게 보아도 마찬가지임을 알 수 있죠?
-====== Discussion 3 ======+====== 개인과제 1 ======
 {{:d.yyk.csv}} {{:d.yyk.csv}}
 <code> <code>
Line 719: Line 722:
  
 <code> <code>
-lm.c1 <- lm(Sales ~ CompPrice, data = Carseats)+# 개인과제와 관련된 코드입니다.  
 +# Carseats 데이터 분석입니다. 새로 시작하면  
 +library(ISLR) 
 +# 만약에 ISLR이 install도 안되어  
 +# 있으면  
 +# install.packages("ISLR"
 + 
 +?Carseats   # 데이터 설명 확인 
 +str(Carseats) 
 + 
 +# 이 중에서 CompPrice의 독립변인으로서의 
 +# 역할에 문제점이 보이는 듯 하여 물어봅니다 
 +# CompPrice와 다른 변인들을 모두 활용해도  
 +# 되겠지만 간단하게 보기 위해서 Price만을 
 +# 독립변인으로 분석을 합니다 
 + 
 +lm.c1 <- lm(Sales ~ Price + CompPrice, data = Carseats)
 summary(lm.c1) summary(lm.c1)
 +
 +# output 을 살펴보면 R 제곱값이 
 +# 0.3578 (35.78%) 임을 알 수 있습니다. 
 +# 이는 우리가 흔히 쓰는 다이어그램에서
 +#
 +# http://commres.net/wiki/_detail/pasted/20201201-170048.png?id=multiple_regression_exercise
 +#
 +# b + c + d 에 해당하는 부분입니다. 즉 두 변인의
 +# 설명력을 보여주는 부분인 lm.c1에서의 
 +# R 제곱은 b + c + d / a + b + c + d 입니다. 
 +
 +# 여기에서 아래를 수행하여 
 +# semipartial corrleation 값과 이를 
 +# 제곱한 값을 알아봅니다. 
 +
 +library(ppcor)
 +attach(Carseats)
 +spcor.Price <- spcor.test(Sales, Price, CompPrice)
 +spcor.CompPrice <- spcor.test(Sales, CompPrice, Price)
 +
 +spcor.Price
 +spcor.CompPrice
 +
 +# 위 둘의 아웃풋에서 estimate값은 
 +# semipartial correlation 값이므로 이를 
 +# 제곱한 값은 각각 b와 d에 해당하는 
 +# 값입니다. 이를 더 자세히 이야기하면
 +# b = b / a + b + c + d
 +# d = d / a + b + c + d
 +# 라는 이야기입니다. 
 +
 +b <- spcor.Price$estimate^2
 +d <- spcor.CompPrice$estimate^2 
 +
 +# 각 b와 d를 출력해 봅니다. 
 +b
 +d
 +
 +# 이 둘을 더해봅니다. 
 +# 이 값은 0.5135791 입니다
 +b + d
 +
 +# 다시 아까 lm.c1의 R 제곱값은 
 +summary(lm.c1)$r.squared
 +# 0.3578332 입니다. 
 +
 +# 다시 그림을 보면
 +# R 제곱은 b + c + d 에 해당하는 
 +# 것이라고 했고
 +# semi-partial 값을 각각 구하여
 +# 이를 제곱하여 더한 것이 b와 d입니다.
 +# 그런데 그림을 보면 b + d > b + c + d 가
 +# 됩니다.  그렇지만 그림대로라면 
 +# 이것은 말이 되지 않습니다. 그런데 
 +# 계산값을 이를 가르키고 있습니다. 
 +# 이 이유가 뭘까요?
 +
 +# 여러분의 생각 후에 mediator 분석에 대해서
 +# 설명을 하도록 하겠습니다. 
    
 </code> </code>
    
 +--> 
 +[[:Suppressor in Multiple Regression]]
 ====== Making Questionnaire ====== ====== Making Questionnaire ======
 [[https://eclass2.ajou.ac.kr/webapps/discussionboard/do/forum?action=list_threads&course_id=_47711_1&nav=discussion_board_entry&conf_id=_52871_1&forum_id=_55560_1|Questions]] you submit at the ajoubb. [[https://eclass2.ajou.ac.kr/webapps/discussionboard/do/forum?action=list_threads&course_id=_47711_1&nav=discussion_board_entry&conf_id=_52871_1&forum_id=_55560_1|Questions]] you submit at the ajoubb.
multiple_regression_exercise.txt · Last modified: 2023/12/10 21:41 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki