====== Question ====== Carseat에서 아주 이상한 일이 있습니다. 이것에 대해서 각 개인의 생각을 묻습니다. # 개인과제와 관련된 코드입니다. # Carseats 데이터 분석입니다. 새로 시작하면 # 만약에 ISLR이 install도 안되어 # 있으면 install.packages("ISLR") library(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 분석에 대해서 # 설명을 하도록 하겠습니다. ====== Exploration ====== # 개인과제와 관련된 코드입니다. # 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 분석에 대해서 # 설명을 하도록 하겠습니다. # # cs.dat <- Carseats[, c(1,2,6)] str(cs.dat) round(cor(cs.dat),3) ia.1 <- lm(Sales~CompPrice, data=cs.dat) ia.2 <- lm(Sales~Price, data=cs.dat) ia.3 <- lm(Sales~CompPrice+Price, data=cs.dat) # Price 에 CompPrice를 더한 것에 따라서 # r square 값의 증가분이 의미가 있는가를 # 테스트 anova(ia.2, ia.3) # 증가분은 summary(ia.3)$r.squared - summary(ia.2)$r.squared # mediated effect = CompPrice에 대한 Price의 설명력이 # 얼마나 Sales를 설명하는가? ia.0 <- lm(CompPrice~Price, data=cs.dat) # CompPrice에 대한 Price의 설명력 med.price.2.compprice <- ia.0$fitted.values - mean(cs.dat$CompPrice) cor(med.price.2.compprice, cs.dat$Sales) cor(cs.dat$Price, cs.dat$Sales) ia.01 <- lm(Sales~med.price.2.compprice, data=cs.dat) summary(ia.01) summary(ia.2) ia.02 <- lm(Sales~ia.0$residuals, data=cs.dat) summary(ia.02) summary(ia.01)$r.squared summary(ia.02)$r.squared summary(ia.01)$r.squared + summary(ia.02)$r.squared summary(ia.3) ====== Explanation ====== > # 개인과제와 관련된 코드입니다. > # Carseats 데이터 분석입니다. 새로 시작하면 > library(ISLR) > # 만약에 ISLR이 install도 안되어 > # 있으면 > # install.packages("ISLR") > > ?Carseats # 데이터 설명 확인 > str(Carseats) 'data.frame': 400 obs. of 11 variables: $ Sales : num 9.5 11.22 10.06 7.4 4.15 ... $ CompPrice : num 138 111 113 117 141 124 115 136 132 132 ... $ Income : num 73 48 35 100 64 113 105 81 110 113 ... $ Advertising: num 11 16 10 4 3 13 0 15 0 0 ... $ Population : num 276 260 269 466 340 501 45 425 108 131 ... $ Price : num 120 83 80 97 128 72 108 120 124 124 ... $ ShelveLoc : Factor w/ 3 levels "Bad","Good","Medium": 1 2 3 3 1 1 3 2 3 3 ... $ Age : num 42 65 59 55 38 78 71 67 76 76 ... $ Education : num 17 10 12 14 13 16 15 10 10 17 ... $ Urban : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 1 2 2 1 1 ... $ US : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 2 1 2 1 2 ... > > > # 이 중에서 CompPrice의 독립변인으로서의 > # 역할에 문제점이 보이는 듯 하여 물어봅니다 > # CompPrice와 다른 변인들을 모두 활용해도 > # 되겠지만 간단하게 보기 위해서 Price만을 > # 독립변인으로 분석을 합니다 > > lm.c1 <- lm(Sales ~ Price + CompPrice, data = Carseats) > summary(lm.c1) Call: lm(formula = Sales ~ Price + CompPrice, data = Carseats) Residuals: Min 1Q Median 3Q Max -5.5285 -1.6207 -0.2404 1.5269 6.2437 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.278692 0.932774 6.731 5.91e-11 *** Price -0.087458 0.005914 -14.788 < 2e-16 *** CompPrice 0.090777 0.009132 9.941 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.269 on 397 degrees of freedom Multiple R-squared: 0.3578, Adjusted R-squared: 0.3546 F-statistic: 110.6 on 2 and 397 DF, p-value: < 2.2e-16 > > # 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) The following objects are masked from Carseats (pos = 3): Advertising, Age, CompPrice, Education, Income, Population, Price, Sales, ShelveLoc, Urban, US The following objects are masked from Carseats (pos = 4): Advertising, Age, CompPrice, Education, Income, Population, Price, Sales, ShelveLoc, Urban, US > spcor.Price <- spcor.test(Sales, Price, CompPrice) > spcor.CompPrice <- spcor.test(Sales, CompPrice, Price) > > spcor.Price estimate p.value statistic n gp Method 1 -0.5947496 1.561477e-39 -14.74081 400 1 pearson > spcor.CompPrice estimate p.value statistic n gp Method 1 0.399815 9.53872e-17 8.691134 400 1 pearson > > # 위 둘의 아웃풋에서 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 [1] 0.3537271 > d [1] 0.159852 > > # 이 둘을 더해봅니다. > # 이 값은 0.5135791 입니다 > b + d [1] 0.5135791 > > # 다시 아까 lm.c1의 R 제곱값은 > summary(lm.c1)$r.squared [1] 0.3578332 > # 0.3578332 입니다. > > # 다시 그림을 보면 > # R 제곱은 b + c + d 에 해당하는 > # 것이라고 했고 > # semi-partial 값을 각각 구하여 > # 이를 제곱하여 더한 것이 b와 d입니다. > # 그런데 그림을 보면 b + d > b + c + d 가 > # 됩니다. 그렇지만 그림대로라면 > # 이것은 말이 되지 않습니다. 그런데 > # 계산값을 이를 가르키고 있습니다. > # 이 이유가 뭘까요? > > # 여러분의 생각 후에 mediator 분석에 대해서 > # 설명을 하도록 하겠습니다. > # > # > > cs.dat <- Carseats[, c(1,2,6)] > str(cs.dat) 'data.frame': 400 obs. of 3 variables: $ Sales : num 9.5 11.22 10.06 7.4 4.15 ... $ CompPrice: num 138 111 113 117 141 124 115 136 132 132 ... $ Price : num 120 83 80 97 128 72 108 120 124 124 ... > round(cor(cs.dat),3) Sales CompPrice Price Sales 1.000 0.064 -0.445 CompPrice 0.064 1.000 0.585 Price -0.445 0.585 1.000 > ia.1 <- lm(Sales~CompPrice, data=cs.dat) > ia.2 <- lm(Sales~Price, data=cs.dat) > ia.3 <- lm(Sales~CompPrice+Price, data=cs.dat) > > # Price 에 CompPrice를 더한 것에 따라서 > # r square 값의 증가분이 의미가 있는가를 > # 테스트 > anova(ia.2, ia.3) Analysis of Variance Table Model 1: Sales ~ Price Model 2: Sales ~ CompPrice + Price Res.Df RSS Df Sum of Sq F Pr(>F) 1 398 2552.2 2 397 2043.5 1 508.69 98.824 < 2.2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > # 증가분은 > summary(ia.3)$r.squared - summary(ia.2)$r.squared [1] 0.159852 > > # mediated effect = CompPrice에 대한 Price의 설명력이 > # 얼마나 Sales를 설명하는가? > > ia.0 <- lm(CompPrice~Price, data=cs.dat) > > # CompPrice에 대한 Price의 설명력 > med.price.2.compprice <- ia.0$fitted.values - mean(cs.dat$CompPrice) > cor(med.price.2.compprice, cs.dat$Sales) [1] -0.4449507 > cor(cs.dat$Price, cs.dat$Sales) [1] -0.4449507 > > ia.01 <- lm(Sales~med.price.2.compprice, data=cs.dat) > summary(ia.01) Call: lm(formula = Sales ~ med.price.2.compprice, data = cs.dat) Residuals: Min 1Q Median 3Q Max -6.5224 -1.8442 -0.1459 1.6503 7.5108 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 7.49633 0.12662 59.205 <2e-16 *** med.price.2.compprice -0.14011 0.01414 -9.912 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.532 on 398 degrees of freedom Multiple R-squared: 0.198, Adjusted R-squared: 0.196 F-statistic: 98.25 on 1 and 398 DF, p-value: < 2.2e-16 > summary(ia.2) Call: lm(formula = Sales ~ Price, data = cs.dat) Residuals: Min 1Q Median 3Q Max -6.5224 -1.8442 -0.1459 1.6503 7.5108 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 13.641915 0.632812 21.558 <2e-16 *** Price -0.053073 0.005354 -9.912 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.532 on 398 degrees of freedom Multiple R-squared: 0.198, Adjusted R-squared: 0.196 F-statistic: 98.25 on 1 and 398 DF, p-value: < 2.2e-16 > ia.02 <- lm(Sales~ia.0$residuals, data=cs.dat) > summary(ia.02) Call: lm(formula = Sales ~ ia.0$residuals, data = cs.dat) Residuals: Min 1Q Median 3Q Max -6.5398 -1.9298 -0.1402 1.7502 7.4355 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 7.49632 0.12959 57.846 <2e-16 *** ia.0$residuals 0.09078 0.01043 8.702 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.592 on 398 degrees of freedom Multiple R-squared: 0.1599, Adjusted R-squared: 0.1577 F-statistic: 75.73 on 1 and 398 DF, p-value: < 2.2e-16 > > summary(ia.01)$r.squared [1] 0.1979812 > summary(ia.02)$r.squared [1] 0.159852 > summary(ia.01)$r.squared + summary(ia.02)$r.squared [1] 0.3578332 > summary(ia.3) Call: lm(formula = Sales ~ CompPrice + Price, data = cs.dat) Residuals: Min 1Q Median 3Q Max -5.5285 -1.6207 -0.2404 1.5269 6.2437 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.278692 0.932774 6.731 5.91e-11 *** CompPrice 0.090777 0.009132 9.941 < 2e-16 *** Price -0.087458 0.005914 -14.788 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.269 on 397 degrees of freedom Multiple R-squared: 0.3578, Adjusted R-squared: 0.3546 F-statistic: 110.6 on 2 and 397 DF, p-value: < 2.2e-16 > >