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/18 16:26] – [추가 설명 2] 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 315: Line 318:
  
 Coefficients: Coefficients:
-                (Intercept)                  Advertising    Advertising:ShelveLocGood  Advertising:ShelveLocMedium   +        (Intercept)                  Advertising    Advertising:ShelveLocGood  Advertising:ShelveLocMedium   
-                    6.76500                     -0.05412                      0.35597                      0.14922  +            6.76500                     -0.05412                      0.35597                      0.14922  
 </code> </code>
  
Line 386: Line 389:
 </code> </code>
 그리고 이렇게 해서 interaction effect는 없다고 보는 것이 맞지만, 선생님이 ShelveLoc이 Advertising과 어떤 관계로 나타나는지 살펴보기 위해서 temporary하게 ShelveLoc 의 main effect를 제외하고 (즉, regression에서 독립변인으로 제외하고), interaction temer만을 (Advertising:ShelveLoc) 추가해본겁니다. 그러면 패턴이 보이니까요. 즉, **광고효과는 진열과 상호작용해서 Sales로 나타난다고 생각해도 된다**고 이야기해보려고 한것입니다.  그리고 이렇게 해서 interaction effect는 없다고 보는 것이 맞지만, 선생님이 ShelveLoc이 Advertising과 어떤 관계로 나타나는지 살펴보기 위해서 temporary하게 ShelveLoc 의 main effect를 제외하고 (즉, regression에서 독립변인으로 제외하고), interaction temer만을 (Advertising:ShelveLoc) 추가해본겁니다. 그러면 패턴이 보이니까요. 즉, **광고효과는 진열과 상호작용해서 Sales로 나타난다고 생각해도 된다**고 이야기해보려고 한것입니다. 
 +
 +아래는 설명을 하지 않고 코드만 적었었는데 이는 아래 둘의 model이 동일한 것임을 보여주기 위한 것입니다.
 <code> <code>
 +lm.2 <- lm(Sales ~ Advertising:ShelveLoc, data = Carseats) # ------- (1)  
 +lm.1 <- lm(Sales ~ Advertising + Advertising:ShelveLoc, data = Carseats) # ------- (2)  
 +</code>
  
 +왜 같은지를 보기 위해서 아래를 보면
 +<code>
 > lm.2 <- lm(Sales ~ Advertising:ShelveLoc, data = Carseats) > lm.2 <- lm(Sales ~ Advertising:ShelveLoc, data = Carseats)
 > lm.2 > lm.2
Line 395: Line 405:
  
 Coefficients: Coefficients:
-                (Intercept)     Advertising:ShelveLocBad    Advertising:ShelveLocGood  Advertising:ShelveLocMedium   +        (Intercept)     Advertising:ShelveLocBad    Advertising:ShelveLocGood  Advertising:ShelveLocMedium   
-                    6.76500                     -0.05412                      0.30185                      0.09510   +            6.76500                     -0.05412                      0.30185                      0.09510  
  
 +</code>
 +1) ShelveLoc Good일 경우, ShelveLOcGood = 1이고 나머지는 0이므로 
 +Sales hat = Intercept + Advertising:ShelveLocGood 이 됩니다. 즉, 
 +Sales hat = 6.76500 + 0.30185 * Advertising
  
 +2) ShelveLoc Medium일 경우는 
 +Sales hat = 6.76500 + 0.09510 * Advertising 이 됩니다
 +
 +3) Bad 인 경우는 
 +Sales hat = 6.76500 + -0.05412 * Advertising 이 됩니다. 
 +
 +위는 결국 lm.1 과 같은 모델이라는 뜻입니다. 
 +
 +참고로 위에서 원래 lm.1에서 우리가 살펴본 각 라인은 아래와 같았었습니다.
 +<code>
 +> abline(a = 6.76500, b = 0.30185, col = "green", lwd=3) # ShelveLoc Good 인경우
 +> abline(a = 6.76500, b = 0.0951, col = "red", lwd=3) # Medium 인 경우
 +> abline(a = 6.76500, b = -0.05412, col = "blue", lwd=3) # Bad 인 경우
 </code> </code>
 +
 ===== 추가 설명 2 ===== ===== 추가 설명 2 =====
 선생님이 처음 예에서는 Advertising을 빼고 interaction을 보고는 바로 위의 설명에서는 ShelveLoc을 빼서 (깜박하고 거꾸로 뺏어요) 좀 당황할 수 있으므로 원래대로 해도 마찬가지라는 걸 조금 보려고 합니다. 이것을 R code로 쓰면 아래와 같습니다. 처음것은 두변인과 두변인의 interaction을 모두 포함한 것이고 그 다음 것은 Advertising 주효과를 제외한 것입니다.  선생님이 처음 예에서는 Advertising을 빼고 interaction을 보고는 바로 위의 설명에서는 ShelveLoc을 빼서 (깜박하고 거꾸로 뺏어요) 좀 당황할 수 있으므로 원래대로 해도 마찬가지라는 걸 조금 보려고 합니다. 이것을 R code로 쓰면 아래와 같습니다. 처음것은 두변인과 두변인의 interaction을 모두 포함한 것이고 그 다음 것은 Advertising 주효과를 제외한 것입니다. 
Line 445: 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 495: Line 522:
  
 그러니 이렇게 보아도 마찬가지임을 알 수 있죠? 그러니 이렇게 보아도 마찬가지임을 알 수 있죠?
-====== Discussion 3 ======+====== 개인과제 1 ======
 {{:d.yyk.csv}} {{:d.yyk.csv}}
 <code> <code>
Line 691: Line 718:
 {{:pasted:20201201-170048.png}} {{:pasted:20201201-170048.png}}
  
 +====== 개인과제 2 ======
 +Carseat에서 아주 이상한 일이 있습니다. 이것에 대해서 각 개인의 생각을 묻습니다. 
  
- +<code> 
 +# 개인과제와 관련된 코드입니다.  
 +# 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)
 +
 +# 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>
 + 
 +-->
 +[[: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