using_dummy_variables
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| using_dummy_variables [2026/06/09 22:37] – hkimscil | using_dummy_variables [2026/06/14 23:15] (current) – [Regression with two Catogorical IVs] hkimscil | ||
|---|---|---|---|
| Line 523: | Line 523: | ||
| meals 6 pct free meals | meals 6 pct free meals | ||
| ell 7 english language learners | ell 7 english language learners | ||
| - | yr_rnd 8 year round school | + | yr_rnd 8 year round school |
| mobility 9 pct 1st year in school | mobility 9 pct 1st year in school | ||
| acs_k3 10 avg class size k-3 | acs_k3 10 avg class size k-3 | ||
| Line 539: | Line 539: | ||
| collcat 22 < | collcat 22 < | ||
| Variables in the working file | Variables in the working file | ||
| + | |||
| + | yr_rnd: | ||
| + | 0 = 방학있음 | ||
| + | 1 = 방학없음 | ||
| mealcat: | mealcat: | ||
| Line 544: | Line 548: | ||
| 2 = 47-80 | 2 = 47-80 | ||
| 3 = 81-100 | 3 = 81-100 | ||
| - | | ||
| </ | </ | ||
| + | |||
| + | * pct (%) of emer (emergency) credentials: | ||
| + | * pct (%) of full credentials: | ||
| 위의 변인들 중에서 " | 위의 변인들 중에서 " | ||
| + | <tabbox rscrp.03> | ||
| + | < | ||
| + | # data 정리 | ||
| + | df <- datavar[, | ||
| + | head(df) | ||
| + | str(df) | ||
| + | df$yr_rnd <- factor(df$yr_rnd, | ||
| + | df$mealcat <- factor(df$mealcat, | ||
| + | str(df) | ||
| - | < | + | # regression |
| - | /dep api00 | + | m.br <- lm(api00~yr_rnd, data=df) |
| - | /method = enter yr_rnd. | + | summary(m.br) |
| - | </ | + | |
| - | | **Model Summary** | + | # making dummy variables |
| - | | Model | R | R Square | + | df$nobr <- ifelse(df$yr_rnd == " |
| - | | 1 | .475a | + | df$br<- ifelse(df$yr_rnd == " |
| - | | a. Predictors: | + | head(df) |
| - | | **ANOVA(b)** ||||||| | + | m.br2 <- lm(api00~nobr, |
| - | | Model | + | summary(m.br2) |
| - | | 1 | Regression | + | |
| - | | | Residual | + | |
| - | | | Total | 8073671.997 | + | |
| - | | a. Predictors: (Constant), year round school | + | |
| + | m.br3 <- lm(api00~br, | ||
| + | summary(m.br3) | ||
| - | | **Coefficients(a)** ||||||| | + | # baseline 변화 |
| - | | | | Unstandardized Coefficients | + | df$yr_rnd <- relevel(factor(df$yr_rnd), |
| - | | Model | + | m.br4 <- lm(api00 ~ yr_rnd, data = df) |
| - | | 1 | + | summary(m.br4) |
| - | | | year round school | + | # 원래로 복원 |
| - | | a. Dependent Variable: api 2000 | + | df$yr_rnd <- relevel(factor(df$yr_rnd), |
| + | head(df) | ||
| + | tmp <- aggregate(df[, | ||
| + | mean.br <- tmp[[2]][1] | ||
| + | mean.nobr <- tmp[[2]][2] | ||
| + | data.frame(mean.br, | ||
| - | 위의 아웃풋을 살펴 보면, | + | boxplot(df$api00~df$yr_rnd) |
| - | 학생들의 성적이 가지는 변량의 약 23%를 방학이없는학교가 갖으며, 이는 통계적으로 유의미한 것이다 | + | |
| - | $\hat{Y} = 684.539 - 160.506 X$ | + | t.out <- t.test(df$api00~df$yr_rnd, |
| - | + | t.out | |
| - | 이 때, | + | t.out$estimate |
| - | * X: 0 = No | + | # str(t.out) |
| - | * X: 1 = Yes | + | t.value <- t.out$statistic |
| - | 이므로 x=0 일때를 대입해 보면, 즉, 방학이있는학교의 경우는 684.539의 추정치를 엊을 수 있으며, x=1일때를 대입해 보면 즉, 방학이없는학교의 경우에는 524.033의 추정치를 엊을 수 있다. b coefficient가 이 역할 (차이를 나타내는 역할을 하는데)에 대한 유의성에 대한 판단은 t-test로 하는데, 이 t값은 -10.782며 이는 F값인 116.241의 제곱근이다 | + | f.out <- aov(df$api00~df$yr_rnd, data=df) |
| + | summary(f.out) | ||
| + | f.value <- summary(f.out)[[1]][1, "F value" | ||
| - | 또한 위에서 이야기한 추정치는 X변인의 특성인 무방학학교과 일반학교의 평균과 같으며, X변인의 coefficient였던 | + | summary(m.br) |
| + | f.value.lm <- summary(m.br)$fstatistic[1] | ||
| + | t.value.lm <- summary(m.br)$coefficients[" | ||
| - | < | + | data.frame(t.value, t.value^2, f.value, f.value.lm, t.value.lm) |
| - | /X1 = VAR(yr_rnd) TYPE = scale | + | |
| - | /Y = VAR (api00) TYPE = SCALE | + | |
| - | / | + | |
| - | / | + | |
| - | / | + | |
| - | </ | + | |
| - | {{regressionCategory.jpg}} | + | plot(df$yr_rnd, |
| - | 위의 그래프에서 직선은 $\hat{Y} | + | df %>% |
| + | ggplot(aes(x | ||
| + | geom_point(size = 3) + | ||
| + | geom_smooth(method = " | ||
| + | scale_color_brewer(palette = " | ||
| + | labs(title = " | ||
| + | x = " (yr_rnd)", | ||
| + | y = " | ||
| + | |||
| + | summary(m.br) | ||
| - | < | ||
| - | TABLES=api00 BY yr_rnd. | ||
| </ | </ | ||
| + | <tabbox rout.03> | ||
| + | < | ||
| + | > # data 정리 | ||
| + | > df <- datavar[, | ||
| + | > head(df) | ||
| + | snum api00 meals ell yr_rnd mobility full mealcat | ||
| + | 1 906 | ||
| + | 2 889 | ||
| + | 3 887 | ||
| + | 4 876 | ||
| + | 5 888 | ||
| + | 6 4284 | ||
| + | > str(df) | ||
| + | ' | ||
| + | $ snum : int 906 889 887 876 888 4284 4271 2910 2899 2887 ... | ||
| + | $ api00 : int 693 570 546 571 478 858 918 831 860 737 ... | ||
| + | $ meals : int 67 92 97 90 89 10 5 2 5 29 ... | ||
| + | $ ell : int 9 21 29 27 30 3 2 3 6 15 ... | ||
| + | $ yr_rnd | ||
| + | $ mobility: int 11 33 36 27 44 10 16 44 10 17 ... | ||
| + | $ full : int 76 79 68 87 87 100 100 96 100 96 ... | ||
| + | $ mealcat : int 2 3 3 3 3 1 1 1 1 1 ... | ||
| + | > df$yr_rnd <- factor(df$yr_rnd, | ||
| + | > df$mealcat <- factor(df$mealcat, | ||
| + | > str(df) | ||
| + | ' | ||
| + | $ snum : int 906 889 887 876 888 4284 4271 2910 2899 2887 ... | ||
| + | $ api00 : int 693 570 546 571 478 858 918 831 860 737 ... | ||
| + | $ meals : int 67 92 97 90 89 10 5 2 5 29 ... | ||
| + | $ ell : int 9 21 29 27 30 3 2 3 6 15 ... | ||
| + | $ yr_rnd | ||
| + | $ mobility: int 11 33 36 27 44 10 16 44 10 17 ... | ||
| + | $ full : int 76 79 68 87 87 100 100 96 100 96 ... | ||
| + | $ mealcat : Factor w/ 3 levels " | ||
| + | > | ||
| + | > # regression | ||
| + | > m.br <- lm(api00~yr_rnd, | ||
| + | > summary(m.br) | ||
| - | | **Report** | + | Call: |
| - | | api 2000 |||| | + | lm(formula = api00 ~ yr_rnd, data = df) |
| - | | year round school | + | |
| - | | No | 684.54 | + | |
| - | | Yes | 524.03 | + | |
| - | | Total | 647.62 | + | |
| - | 이와 같이 종류변인(category, | + | Residuals: |
| + | | ||
| + | -273.539 | ||
| - | ===== 3 or more groups ===== | + | Coefficients: |
| - | 만약에 ANOVA 테스트에서와 같이 종류가 3개 이상인 변인은 어떻게 처리해야 할까? 아래는 이를 regression으로 테스트 한 결과이다. | + | |
| + | (Intercept) | ||
| + | yr_rndnobr | ||
| + | --- | ||
| + | Signif. codes: | ||
| - | <code> | + | Residual standard error: 125.3 on 398 degrees of freedom |
| - | > mod2 <- lm(api00 ~ factor(mealcat), data=datavar) | + | Multiple R-squared: |
| - | > mod2 | + | F-statistic: |
| + | |||
| + | > | ||
| + | > # making dummy variables | ||
| + | > df$nobr <- ifelse(df$yr_rnd == " | ||
| + | > df$br<- ifelse(df$yr_rnd == " | ||
| + | > head(df) | ||
| + | snum api00 meals ell yr_rnd mobility full mealcat nobr br | ||
| + | 1 906 | ||
| + | 2 889 | ||
| + | 3 887 | ||
| + | 4 876 | ||
| + | 5 888 | ||
| + | 6 4284 | ||
| + | > | ||
| + | > m.br2 <- lm(api00~nobr,data=df) | ||
| + | > summary(m.br2) | ||
| Call: | Call: | ||
| - | lm(formula = api00 ~ factor(mealcat), data = datavar) | + | lm(formula = api00 ~ nobr, data = df) |
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -273.539 | ||
| Coefficients: | Coefficients: | ||
| - | (Intercept) | + | Estimate Std. Error t value Pr(>|t|) |
| - | 805.7 -166.3 | + | (Intercept) 684.54 7.14 |
| + | nobr -160.51 14.89 | ||
| + | --- | ||
| + | Signif. codes: | ||
| - | > summary(mod2) | + | Residual standard error: 125.3 on 398 degrees of freedom |
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > | ||
| + | > m.br3 <- lm(api00~br, | ||
| + | > summary(m.br3) | ||
| Call: | Call: | ||
| - | lm(formula = api00 ~ factor(mealcat), | + | lm(formula = api00 ~ br, data = df) |
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -273.539 | ||
| + | |||
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | br 160.51 | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 125.3 on 398 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > | ||
| + | > # baseline 변화 | ||
| + | > df$yr_rnd <- relevel(factor(df$yr_rnd), ref = " | ||
| + | > m.br4 <- lm(api00 ~ yr_rnd, data = df) | ||
| + | > summary(m.br4) | ||
| + | |||
| + | Call: | ||
| + | lm(formula = api00 ~ yr_rnd, data = df) | ||
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -273.539 | ||
| + | |||
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | yr_rndbreak | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 125.3 on 398 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > # 원래로 복원 | ||
| + | > df$yr_rnd <- relevel(factor(df$yr_rnd), | ||
| + | > | ||
| + | > head(df) | ||
| + | snum api00 meals ell yr_rnd mobility full mealcat | ||
| + | 1 906 | ||
| + | 2 889 | ||
| + | 3 887 | ||
| + | 4 876 | ||
| + | 5 888 | ||
| + | 6 4284 | ||
| + | > tmp <- aggregate(df[, | ||
| + | > mean.br <- tmp[[2]][1] | ||
| + | > mean.nobr <- tmp[[2]][2] | ||
| + | > data.frame(mean.br, | ||
| + | mean.br mean.nobr | ||
| + | 1 684.539 | ||
| + | > | ||
| + | > boxplot(df$api00~df$yr_rnd) | ||
| + | > | ||
| + | > t.out <- t.test(df$api00~df$yr_rnd, | ||
| + | > t.out | ||
| + | |||
| + | Two Sample t-test | ||
| + | |||
| + | data: df$api00 by df$yr_rnd | ||
| + | t = 10.782, df = 398, p-value < 2.2e-16 | ||
| + | alternative hypothesis: true difference in means between group break and group nobr is not equal to 0 | ||
| + | 95 percent confidence interval: | ||
| + | | ||
| + | sample estimates: | ||
| + | mean in group break mean in group nobr | ||
| + | | ||
| + | |||
| + | > t.out$estimate | ||
| + | mean in group break mean in group nobr | ||
| + | | ||
| + | > # str(t.out) | ||
| + | > t.value <- t.out$statistic | ||
| + | > | ||
| + | > f.out <- aov(df$api00~df$yr_rnd, | ||
| + | > summary(f.out) | ||
| + | | ||
| + | df$yr_rnd | ||
| + | Residuals | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | > f.value <- summary(f.out)[[1]][1, | ||
| + | > | ||
| + | > summary(m.br) | ||
| + | |||
| + | Call: | ||
| + | lm(formula = api00 ~ yr_rnd, data = df) | ||
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -273.539 | ||
| + | |||
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | yr_rndnobr | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 125.3 on 398 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > f.value.lm <- summary(m.br)$fstatistic[1] | ||
| + | > t.value.lm <- summary(m.br)$coefficients[" | ||
| + | > | ||
| + | > data.frame(t.value, | ||
| + | t.value t.value.2 | ||
| + | t 10.7815 | ||
| + | > | ||
| + | > plot(df$yr_rnd, | ||
| + | > | ||
| + | > df %>% | ||
| + | + | ||
| + | + | ||
| + | + | ||
| + | + | ||
| + | + | ||
| + | + x = " (yr_rnd)", | ||
| + | + y = " | ||
| + | `geom_smooth()` using formula = 'y ~ x' | ||
| + | > | ||
| + | > summary(m.br) | ||
| + | |||
| + | Call: | ||
| + | lm(formula = api00 ~ yr_rnd, data = df) | ||
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -273.539 | ||
| + | |||
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | yr_rndnobr | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 125.3 on 398 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > | ||
| + | </ | ||
| + | </ | ||
| + | {{pasted: | ||
| + | {{pasted: | ||
| + | 위의 아웃풋을 살펴 보면, 학생들의 성적이 가지는 총 변량의 (Sum of Square Total) 약 22.6% 를 방학이 있고 없고로 구분되는 yr_rnd 변인이 설명을 하고 있으며, 이는 통계적으로 유의미한 것이다 (F(1, 398) = 116.241, p < .001). 위의 regression output은 yr_rnd 변인 중에서 방학이 있는 특성이 baseline이 되어 있으며, 이를 염두에 두고 regression식을 적어 보면 아래와 같다. | ||
| + | |||
| + | '' | ||
| + | * yr_rndno_break: | ||
| + | * y hat = 684.54 - 160.51 * (1) | ||
| + | * y hat = 524.03 | ||
| + | * yr_rndbreak: | ||
| + | * y hat = 684.54 - 160.51 * (0) | ||
| + | * y hat = 684.54 | ||
| + | |||
| + | 위 회귀식에서 r은 | ||
| + | '' | ||
| + | '' | ||
| + | break일 경우에는 | ||
| + | '' | ||
| + | * t.value | ||
| + | * F.value | ||
| + | * t.value.lm | ||
| + | * F.value.lm 은 모두 같은 원리로 두 그룹을 (집단의 api 평균을) 테스트 한 것이다. | ||
| + | |||
| + | {{regressionCategory.jpg}} | ||
| + | |||
| + | 위의 그래프는 두 그룹의 (break, no break 학교그룹) api 점수 분포를 나타내 주는 것이다. 그리고 직선은 두 그룹 평균을 연결한 선으로 $\hat{Y} = 684.539 - 160.506 X$ 으로 표현된다. | ||
| + | |||
| + | 이와 같이 종류변인(category, | ||
| + | |||
| + | ===== Regression with a categorical IV with 3 attributes ===== | ||
| + | <tabbox rs.3att> | ||
| + | < | ||
| + | m.mealcat <- lm(api00 ~ mealcat, data=df) | ||
| + | summary(m.mealcat) | ||
| + | </ | ||
| + | <tabbox ro.3att> | ||
| + | < | ||
| + | > ####################################### | ||
| + | > # categorical IV with 3 or more attributes | ||
| + | > ####################################### | ||
| + | > m.mealcat <- lm(api00 ~ mealcat, data=df) | ||
| + | > summary(m.mealcat) | ||
| + | |||
| + | Call: | ||
| + | lm(formula = api00 ~ mealcat, data = df) | ||
| Residuals: | Residuals: | ||
| Line 637: | Line 922: | ||
| Coefficients: | Coefficients: | ||
| - | Estimate Std. Error t value Pr(> | + | Estimate Std. Error t value Pr(> |
| - | (Intercept) | + | (Intercept) |
| - | factor(mealcat)2 | + | mealcatto80 |
| - | factor(mealcat)3 | + | mealcatto100 |
| --- | --- | ||
| Signif. codes: | Signif. codes: | ||
| Line 650: | Line 935: | ||
| > | > | ||
| </ | </ | ||
| + | </ | ||
| - | 아까와 같은 두 집단 간의 비교는 가능하였지만, | + | < |
| + | y hat = 805.718 - 166.324 * to80 - 301.338 * to100 | ||
| + | mealcat0-46 (to46 으로 | ||
| + | mealcat47-80 (to80 으로 | ||
| + | maelcat81-100 (to100 | ||
| + | </ | ||
| - | SPSS의 경우에는 아래와 같이 recode작업을 할 수 있다. | + | 이에 대한 해석도 앞에서의 것과 마찬가지이다. |
| + | * y hat = 805.718 - 166.324*mg2 - 301.338*mg3 | ||
| + | * mg1 = 1, mg2 = 0, mg3 = 0 일 경우 | ||
| + | * y hat = 805.718 - 166.324*(0) - 301.338*(0) | ||
| + | * y hat = 805.718 | ||
| + | * mg1 = 0, mg2 = 1, mg3 = 0 일 경우 | ||
| + | * y hat = 805.718 - 166.324*(1) - 301.338*(0) | ||
| + | * y hat = 805.718 - 166.324 | ||
| + | * y hat = 639.394 | ||
| + | * mg1 = 0, mg2 = 0, mg3 = 1 일 경우 | ||
| + | * y hat = 805.718 - 166.324*(0) - 301.338*(1) | ||
| + | * y hat = 805.718 - 301.338 | ||
| + | * y hat = 504.38 | ||
| - | < | + | * 즉, 무료급식의 퍼센티지가 높을 수록 api점수가 낮음을 알 수 있다. 이렇게 무료급식 퍼센티지를 독립변인으로 종속변인인 api00점수를 (학력점수) 봤을 때, 그 설명력이 통계학적으로 유효한가는 regression output에서 (summary(mod2)) |
| - | if mealcat = 1 mealcat1 | + | * F-value 와 p-value를 가지고 판단한다. |
| - | compute mealcat2 = 0. | + | * (F (2, 397) = 611.1; p-value < 2.2e-16) |
| - | if mealcat = 2 mealcat2 = 1. | + | * 위에서 2, 397 은 각각 between degrees of freedom 과 within degrees of freedom 이다. 이를 보고도 우리는 |
| - | compute mealcat3 | + | * 총 400개의 학교가 데이터에 참여했음을 알 수 있고 (2 + 397 에 1을 더한 값), |
| - | if mealcat = 3 mealcat3 = 1. | + | * 독립변인의 종류가 3가지 (df = 2 이므로) 임을 알 수 있다. |
| - | execute. | + | * R square value 는 설명력의 크기를 알려준다. |
| - | </ | + | * 0.7548 즉, 75.48% 를 독립변인이 종속변인을 설명한다 (상당한 크기임을 알 수 있다). |
| - | 위는 해당 카데고리를 1로 만들고, 나머지를 0으로 만들어서 2분화 하는 작업이다. 이렇게 하면 3개의 새로운 변인이 만들어지게 되는데 (mealcat1, mealcat2, mealcat3), 이 세개의 변인 중에서 2개만을 취해서 regression 테스트를 한다. SPSS의 경우에는 | ||
| - | < | + | ===== Regression with a continuous and a categorical IV ===== |
| - | /dependent api00 | + | <tabbox rs.04> |
| - | /method = enter mealcat2 mealcat3. | + | < |
| - | 주의. 세개의 변인을 모두 넣지 않는다. | + | ####################################### |
| - | </code> | + | ####################################### |
| + | # with a continuous and a category IV | ||
| + | ####################################### | ||
| + | ####################################### | ||
| + | m.mealsyr_rnd | ||
| + | summary(m.mealsyr_rnd) | ||
| + | m.mealsxyr_rnd <- lm(api00~meals*yr_rnd, | ||
| + | summary(m.mealsxyr_rnd) | ||
| - | < | + | # 중요: 위에서 yr_rnd의 설명력이 t test를 보면 |
| - | Model R R Square Adjusted R Square Std. Error of the Estimate | + | # 사라짐. 왜? |
| - | 1 .869a .755 .754 70.612 | + | # interaction을 추가하면서 yr_rnd의 설명력은 |
| - | a. Predictors: | + | # (방학이 있고 없음의 설명력은) meals의 percentage가 |
| + | # 0 이 될때의 것으로 설정된다. 0이 되는 데이터가 | ||
| + | # 극단치이고 이 경우에 nobr인 학교도 없으므로 계산된 | ||
| + | # se값이 극대화되어 t값이 작아지게 된다. 이를 바로 | ||
| + | # 잡으려면 meals의 0이 중간에 가도록 한 후에 | ||
| + | # regression을 하면 meals가 0 일때가 극단치가 되지 | ||
| + | # 않으므로 결과가 옳게 나온다 | ||
| - | ANOVA(b) | + | df %>% |
| - | Model Sum of Squares df Mean Square F Sig. | + | ggplot(aes(x = meals, y = api00, color = factor(yr_rnd))) + |
| - | 1 Regression 6094197.670 2 3047098.835 611.121 .000a | + | |
| - | Residual 1979474.328 397 4986.081 | + | |
| - | Total 8073671.997 399 | + | |
| - | a. Predictors: (Constant), mealcat3, mealcat2 | + | |
| - | b. Dependent Variable: api 2000 | + | x = " |
| + | y = " | ||
| - | Coefficients(a) | ||
| - | Unstandardized Coefficients Standardized Coefficients | ||
| - | Model B Std. Error Beta t Sig. | ||
| - | 1 (Constant) 805.718 6.169 130.599 .000 | ||
| - | mealcat2 -166.324 8.708 -.550 -19.099 .000 | ||
| - | mealcat3 -301.338 8.629 -1.007 -34.922 .000 | ||
| - | a. Dependent Variable: api 2000 | ||
| - | </code> | + | # 1. 평균을 빼준 값을 새로운 변인으로 저장 |
| + | df$meals_centered | ||
| + | df$meals - mean(df$meals) | ||
| - | | **Coefficients(a)** ||||||| | + | # 2. Run the model again using the centered variable |
| - | | | | Unstandardized Coefficients | + | m_centered <- lm(api00 ~ meals_centered |
| - | | Model | + | data = df) |
| - | | 1 | (Constant) | 805.718 | + | |
| - | | | mealcat2 | + | |
| - | | | mealcat3 | + | |
| - | | a. Dependent Variable: api 2000 | + | |
| - | 위에서 | + | # 3. Check the new summary |
| - | $\hat{Y} = 805.718 - 166.324 \; \text{mealcat2} - 301.338 \; \text{mealcat3} $ | + | summary(m_centered) |
| - | 이에 대한 해석은 | + | df %>% |
| - | | + | |
| - | | + | |
| - | | + | geom_smooth(method = " |
| - | | + | |
| + | | ||
| + | x = " (free meals percentage CENTERED)", | ||
| + | y = " | ||
| - | < | ||
| - | api 2000 | ||
| - | Percentage free meals in 3 categories Mean N Std. Deviation | ||
| - | 0-46% free meals 805.72 131 65.669 | ||
| - | 47-80% free meals 639.39 132 82.135 | ||
| - | 81-100% free meals 504.38 137 62.727 | ||
| - | Total 647.62 400 142.249 | ||
| - | </ | ||
| - | 위에서, mealcat1대신에 mealcat3 그룹을 빼고 사용했어도, | + | # Install the package if you do not have it |
| + | # install.packages(" | ||
| - | 마지막으로, | + | # Plot the interaction |
| + | library(interactions) | ||
| + | interact_plot(m.mealsxyr_rnd, | ||
| - | <code>glm | + | |
| - | api00 by mealcat | + | |
| - | /print=parameter. | + | m.ellyr_rnd |
| + | summary(m.ellyr_rnd) | ||
| + | m.ellxyr_rnd <- lm(api00~ell*yr_rnd, | ||
| + | summary(m.ellxyr_rnd) | ||
| + | df %>% | ||
| + | | ||
| + | geom_point(size = 3) + | ||
| + | geom_smooth(method = " | ||
| + | scale_color_brewer(palette = " | ||
| + | labs(title = " | ||
| + | x = " (ell, english language learner)", | ||
| + | y = " | ||
| + | |||
| + | # ell mean centred 이번 경우는 큰 차이를 보이지 않는다 | ||
| + | df$ell_centered <- df$ell - mean(df$ell) | ||
| + | m.ell.centered <- lm(api00 ~ ell_centered * yr_rnd, data = df) | ||
| + | summary(m.ell.centered) | ||
| + | |||
| + | coefs <- summary(m.ellxyr_rnd)$coefficients | ||
| + | coefs | ||
| + | api.br <- coefs[1] | ||
| + | api.nobr <- coefs[1]+coefs[3] | ||
| + | slope.red <- coefs[2] | ||
| + | slope.blue <- coefs[2]+coefs[4] | ||
| + | data.frame(api.br, | ||
| + | # br과 nobr에 따라서 ell의 slope가 달라지는것이 | ||
| + | # interaction effects | ||
| </ | </ | ||
| + | <tabbox ro.04> | ||
| + | < | ||
| + | > ####################################### | ||
| + | > ####################################### | ||
| + | > # with a continuous and a category IV | ||
| + | > ####################################### | ||
| + | > ####################################### | ||
| + | > m.mealsyr_rnd <- lm(api00~meals+yr_rnd, | ||
| + | > summary(m.mealsyr_rnd) | ||
| - | | **Between-Subjects Factors** | + | Call: |
| - | | | | Value Label | N | | + | lm(formula = api00 ~ meals + yr_rnd, data = df) |
| - | | Percentage free meals in 3 categories | + | |
| - | | | 2 | 47-80% free meals | 132 | | + | |
| - | | | 3 | 81-100% free meals | 137 | | + | |
| - | | **Tests of Between-Subjects Effects** | + | Residuals: |
| - | | Dependent Variable:api 2000 | + | Min |
| - | | Source | + | -174.882 -41.542 |
| - | | Corrected Model | 6.094E6 | 2 | 3047098.835 | + | |
| - | | Intercept | + | |
| - | | mealcat | + | |
| - | | Error | 1979474.328 | + | |
| - | | Total | 1.758E8 | + | |
| - | | Corrected Total | 8073671.997 | + | |
| - | |a. R Squared = .755 (Adjusted R Squared = .754) | + | |
| - | | **Parameter Estimates** | + | Coefficients: |
| - | | Dependent Variable:api 2000 | + | |
| - | | | | | | | 95% Confidence Interval | + | (Intercept) 885.6192 6.4712 136.855 < 2e-16 *** |
| - | | Parameter | + | meals -3.7921 0.1036 -36.595 < 2e-16 *** |
| - | | Intercept | + | yr_rndnobr |
| - | | [mealcat=1] | + | --- |
| - | | [mealcat=2] | + | Signif. codes: |
| - | | [mealcat=3] | + | |
| - | | a. This parameter is set to zero because it is redundant. | + | |
| - | 혹은 Oneway ANOVA | + | Residual standard error: 59.99 on 397 degrees of freedom |
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| - | <code> | + | > |
| - | / | + | > m.mealsxyr_rnd |
| - | /PLOT MEANS | + | > summary(m.mealsxyr_rnd) |
| - | /MISSING ANALYSIS | + | |
| - | / | + | |
| - | </ | + | |
| - | | | Sum of Squares | + | Call: |
| - | | Between Groups | + | lm(formula = api00 ~ meals * yr_rnd, data = df) |
| - | | Within Groups | + | |
| - | | Total | 8073671.998 | + | |
| + | Residuals: | ||
| + | Min 1Q Median | ||
| + | -175.78 | ||
| - | ===== 2 variables, categorical ===== | + | Coefficients: |
| - | 위에서 사용된 __2 개의 독립변인을 모두__ 넣어서 regression을 할 수도 있다. 위에서 언급한 경로를 따른다면, | + | Estimate Std. Error t value Pr(> |
| + | (Intercept) | ||
| + | meals | ||
| + | yr_rndnobr | ||
| + | meals: | ||
| + | --- | ||
| + | Signif. codes: | ||
| - | <code> | + | Residual standard error: 60.06 on 396 degrees of freedom |
| - | /dep api00 | + | Multiple R-squared: |
| - | /method = | + | F-statistic: |
| + | |||
| + | > | ||
| + | > # 중요: 위에서 yr_rnd의 설명력이 t test를 보면 | ||
| + | > # 사라짐. 왜? | ||
| + | > # interaction을 추가하면서 yr_rnd의 설명력은 | ||
| + | > # (방학이 있고 없음의 설명력은) meals의 percentage가 | ||
| + | > # 0 이 될때의 것으로 설정된다. 0이 되는 데이터가 | ||
| + | > # 극단치이고 이 경우에 nobr인 학교도 없으므로 계산된 | ||
| + | > # se값이 극대화되어 t값이 작아지게 된다. 이를 바로 | ||
| + | > # 잡으려면 meals의 0이 중간에 가도록 한 후에 | ||
| + | > # regression을 하면 meals가 0 일때가 극단치가 되지 | ||
| + | > # 않으므로 결과가 옳게 나온다 | ||
| + | > | ||
| + | > df %>% | ||
| + | + | ||
| + | + | ||
| + | + | ||
| + | + | ||
| + | + | ||
| + | + x = " (free meals percentage)", | ||
| + | + y = " | ||
| + | `geom_smooth()` using formula = 'y ~ x' | ||
| + | > | ||
| + | > | ||
| </ | </ | ||
| + | {{pasted: | ||
| - | | **Model Summary** | + | < |
| - | | Model | + | > # 1. 평균을 빼준 값을 새로운 변인으로 저장 |
| - | | 1 | + | > df$meals_centered <- |
| - | | a. Predictors: | + | + |
| + | > | ||
| + | > # 2. Run the model again using the centered variable | ||
| + | > m_centered <- lm(api00 ~ meals_centered * yr_rnd, | ||
| + | + data = df) | ||
| + | > | ||
| + | > # 3. Check the new summary | ||
| + | > summary(m_centered) | ||
| - | | **ANOVA(b)** ||||||| | + | Call: |
| - | | Model | + | lm(formula = api00 ~ meals_centered |
| - | | 1 | Regression | + | |
| - | | | Residual | + | |
| - | | | Total | 8073671.997 | + | |
| - | | a. Predictors: (Constant), mealcat2, year round school, mealcat1 | + | |
| - | | b. Dependent Variable: api 2000 | + | |
| - | ^ **Coefficients(a)** | + | Residuals: |
| - | | | | Unstandardized \\ Coefficients | + | |
| - | | Model | + | -175.78 |
| - | | 1 | (Constant) | + | |
| - | | | year round school | + | |
| - | | | mealcat1 | + | |
| - | | | mealcat2 | + | |
| - | | a. Dependent Variable: api 2000 | + | |
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | meals_centered | ||
| + | yr_rndnobr | ||
| + | meals_centered: | ||
| + | --- | ||
| + | Signif. codes: | ||
| - | 똑같은 분석이지만 뒤의 두 변인의 효과를 따로 보기 위해서 뽑은 결과이다. | + | Residual standard error: 60.06 on 396 degrees of freedom |
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| - | <code>regression | + | > |
| - | /dep api00 | + | > df %>% |
| - | /method = | + | + |
| - | / | + | + |
| + | + | ||
| + | + | ||
| + | + | ||
| + | + x = " | ||
| + | + y = " | ||
| + | `geom_smooth()` using formula = 'y ~ x' | ||
| + | > | ||
| + | > | ||
| </ | </ | ||
| + | {{pasted: | ||
| - | ^ Model Summary | + | < |
| - | | Model | R | R Square | + | > # Install |
| - | | 1 | + | > # install.packages(" |
| - | | 2 | .876b | .767 | .765 | 68.893 | + | > |
| - | | a. Predictors: | + | > # Plot the interaction |
| + | > library(interactions) | ||
| + | > interact_plot(m.mealsxyr_rnd, pred = meals, modx = yr_rnd) | ||
| + | > | ||
| + | > | ||
| + | > | ||
| + | </ | ||
| + | {{pasted: | ||
| + | < | ||
| + | > m.ellyr_rnd <- lm(api00~ell+yr_rnd, | ||
| + | > summary(m.ellyr_rnd) | ||
| - | | ANOVA(d) | | | | | | | | | | + | Call: |
| - | | Model | + | lm(formula = api00 ~ ell + yr_rnd, data = df) |
| - | | 1 | Regression | + | |
| - | | | Residual | + | |
| - | | | Total | + | |
| - | | 2 | Subset Tests | mealcat1, mealcat2 | + | |
| - | | | Regression | + | |
| - | | | Residual | + | |
| - | | | Total | + | |
| - | | a. Predictors: | + | |
| + | Residuals: | ||
| + | | ||
| + | -265.843 | ||
| - | ^ Coefficients(a) ^^^^^^^ | + | Coefficients: |
| - | | | | Unstandardized Coefficients | + | |
| - | | Model | + | (Intercept) 784.3978 7.2878 107.63 < 2e-16 *** |
| - | | 1 | + | ell |
| - | | | year round school | + | yr_rndnobr |
| - | | 2 | (Constant) | + | --- |
| - | | | year round school | + | Signif. codes: |
| - | | | mealcat1 | + | |
| - | | | mealcat2 | + | |
| - | | a. Dependent Variable: api 2000 | + | |
| + | Residual standard error: 90.1 on 397 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| - | ^ | + | > m.ellxyr_rnd <- lm(api00~ell*yr_rnd, data=df) |
| - | | | | | | | | Collinearity Statistics | + | > summary(m.ellxyr_rnd) |
| - | | Model | + | |
| - | | 1 | mealcat1 | + | |
| - | | | mealcat2 | + | |
| - | | a. Predictors in the Model: (Constant), year round school | + | |
| + | Call: | ||
| + | lm(formula = api00 ~ ell * yr_rnd, data = df) | ||
| - | 해석에 대해서 | + | Residuals: |
| - | ^ **interpretation** | + | |
| - | | | mealcat=1 | + | -270.514 -57.800 5.994 65.845 206.275 |
| - | |yr_rnd=0 | + | |
| - | |yr_rnd=1 | + | |
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | ell -4.4418 | ||
| + | yr_rndnobr | ||
| + | ell: | ||
| + | --- | ||
| + | Signif. codes: | ||
| - | ^ **interpretation** | + | Residual standard error: 89.08 on 396 degrees of freedom |
| - | | | mealcat=1 | + | Multiple R-squared: |
| - | | | mealcat=1->1 | mealcat=2-> | + | F-statistic: 207.1 on 3 and 396 DF, |
| - | | yr_rnd=0 | + | |
| - | | ::: | intercept + \\ BMealCat1 | + | |
| - | | yr_rnd=1 | + | |
| - | | ::: | intercept + \\ BMealCat1 + \\ Byr_rnd | + | |
| - | <code>glm | + | > df %>% |
| - | api00 BY yr_rnd | + | + |
| - | / | + | + |
| - | / | + | + |
| - | </code> | + | + |
| + | + | ||
| + | + x = " | ||
| + | + y = " | ||
| + | `geom_smooth()` using formula = 'y ~ x' | ||
| + | > | ||
| + | > | ||
| + | > # ell mean centred 이번 경우는 큰 차이를 보이지 않는다 | ||
| + | > df$ell_centered | ||
| + | > m.ell.centered <- lm(api00 ~ ell_centered * yr_rnd, data = df) | ||
| + | > summary(m.ell.centered) | ||
| - | ===== continuous + categorical variables ===== | + | Call: |
| - | < | + | lm(formula |
| - | / | + | |
| - | / | + | |
| - | /save pre. | + | |
| - | * pre = predicted value (y hat). | + | |
| - | output: | + | Residuals: |
| + | | ||
| + | -270.514 | ||
| - | Model Summary(b) | + | Coefficients: |
| - | Model R R Square Adjusted R Square Std. Error of the Estimate | + | |
| - | 1 .507a .257 .253 122.951 | + | (Intercept) |
| - | a. Predictors: (Constant), parent some college, year round school | + | ell_centered |
| - | b. Dependent Variable: api 2000 | + | yr_rndnobr |
| + | ell_centered:yr_rndnobr | ||
| + | --- | ||
| + | Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 | ||
| - | ANOVA(b) | + | Residual standard error: 89.08 on 396 degrees of freedom |
| - | Model Sum of Squares df Mean Square F Sig. | + | Multiple R-squared: |
| - | 1 Regression 2072201.839 2 1036100.919 68.539 .000a | + | F-statistic: |
| - | Residual 6001470.159 397 15117.053 | + | > |
| - | Total 8073671.997 399 | + | > |
| - | a. Predictors: | + | > coefs <- summary(m.ellxyr_rnd)$coefficients |
| - | b. Dependent Variable: | + | > coefs |
| + | Estimate Std. Error t value Pr(>|t|) | ||
| + | (Intercept) | ||
| + | ell -4.441819 | ||
| + | yr_rndnobr | ||
| + | ell: | ||
| + | > api.br <- coefs[1] | ||
| + | > api.nobr <- coefs[1]+coefs[3] | ||
| + | > slope.red <- coefs[2] | ||
| + | > slope.blue <- coefs[2]+coefs[4] | ||
| + | > data.frame(api.br, api.nobr, slope.red, slope.blue) | ||
| + | api.br api.nobr slope.red slope.blue | ||
| + | 1 794.2576 683.6798 -4.441819 | ||
| + | > # br과 nobr에 따라서 ell의 slope가 달라지는것이 | ||
| + | > # interaction effects | ||
| + | > | ||
| + | </ | ||
| + | {{pasted: | ||
| - | Coefficients(a) | + | </ |
| - | Unstandardized Coefficients Standardized Coefficients | + | |
| - | Model B Std. Error Beta t Sig. | + | |
| - | 1 (Constant) 637.858 13.503 47.237 .000 | + | |
| - | year round school -149.159 14.875 -.442 -10.027 .000 | + | |
| - | parent some college 2.236 553 .178 4.044 .000 | + | |
| - | a. Dependent Variable: api 2000 | + | |
| - | </code> | + | ===== Regression with a categorical and a continuous IV: e.g. 2 ===== |
| + | <tabbox rs.06> | ||
| + | < | ||
| + | ############################### | ||
| + | # 다른 예 | ||
| + | ############################### | ||
| + | m.ellmealcat <- lm(api00~ell+mealcat, | ||
| + | summary(m.ellmealcat) | ||
| + | # install.packages(" | ||
| + | # library(emmeans) | ||
| + | # Calculate estimated marginal means for each group | ||
| + | gmeans <- emmeans(m.ellmealcat, | ||
| + | # Perform pairwise comparisons using Tukey' | ||
| + | pairs(gmeans, | ||
| + | m.ellxmealcat <- lm(api00~ell*mealcat, | ||
| + | summary(m.ellxmealcat) | ||
| + | df %>% | ||
| + | ggplot(aes(x = ell, y = api00, color = factor(mealcat))) + | ||
| + | geom_point(size = 3) + | ||
| + | geom_smooth(method = " | ||
| + | scale_color_brewer(palette = " | ||
| + | labs(title = " | ||
| + | x = " (ell, english language learner)", | ||
| + | y = " | ||
| + | |||
| + | mealcat1 <- summary(m.ellxmealcat)$coefficients[[1]] | ||
| + | mealcat2 <- summary(m.ellxmealcat)$coefficients[[1]] + | ||
| + | summary(m.ellxmealcat)$coefficients[[3]] | ||
| + | mealcat3 <- summary(m.ellxmealcat)$coefficients[[1]] + | ||
| + | summary(m.ellxmealcat)$coefficients[[4]] | ||
| + | data.frame(mealcat1, | ||
| + | |||
| + | ell.slope.mealcat1 <- summary(m.ellxmealcat)$coefficients[[2]] | ||
| + | ell.slope.mealcat2 <- summary(m.ellxmealcat)$coefficients[[2]] + | ||
| + | summary(m.ellxmealcat)$coefficients[[5]] | ||
| + | ell.slope.mealcat3 <- summary(m.ellxmealcat)$coefficients[[2]] + | ||
| + | summary(m.ellxmealcat)$coefficients[[6]] | ||
| + | data.frame(ell.slope.mealcat1, | ||
| + | | ||
| + | | ||
| - | < | ||
| - | FILTER BY filt. | ||
| - | regress | ||
| - | /dep = api00 | ||
| - | / | ||
| </ | </ | ||
| + | <tabbox ro.06> | ||
| + | < | ||
| + | > ############################### | ||
| + | > # 다른 예 | ||
| + | > ############################### | ||
| + | > m.ellmealcat <- lm(api00~ell+mealcat, | ||
| + | > summary(m.ellmealcat) | ||
| - | 위의 명령어는 | + | Call: |
| - | 필터링되지 않은 케이스들은 버려지게 되어 필터링이 된 케이스들만 선택이 되어 분석에 사용됨을 뜻 한다. 즉, 위는 rn_rnd값이 0 인 케이스에 대해서만 simple regression을 하라는 것이다. | + | lm(formula = api00 ~ ell + mealcat, data = df) |
| - | < | + | Residuals: |
| - | Model R R Square Adjusted R Square Std. Error of the Estimate | + | |
| - | 1 .126a .016 .013 131.278 | + | -224.96 -42.70 -0.32 48.93 179.92 |
| - | a. Predictors: (Constant), parent some college | + | |
| - | ANOVA(b) | + | Coefficients: |
| - | Model Sum of Squares df Mean Square F Sig. | + | |
| - | 1 Regression 84700.858 1 84700.858 4.915 .027a | + | (Intercept) |
| - | Residual 5273591.675 306 17233.960 | + | ell -1.546 0.203 -7.616 1.94e-13 *** |
| - | Total 5358292.532 307 | + | mealcatto80 |
| - | a. Predictors: (Constant), parent some college | + | mealcatto100 -230.737 |
| - | b. Dependent Variable: api 2000 | + | --- |
| + | Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 | ||
| - | Coefficients(a) | + | Residual standard error: 66.03 on 396 degrees of freedom |
| - | Unstandardized Coefficients Standardized Coefficients | + | Multiple R-squared: |
| - | Model B Std. Error Beta t Sig. | + | F-statistic: |
| - | 1 (Constant) 655.110 15.237 42.995 .000 | + | |
| - | parent some college 1.409 .636 .126 2.217 .027 | + | |
| - | a. Dependent Variable: api 2000 | + | |
| - | </code> | + | |
| - | {{regression-cat0.jpg}} | + | > # install.packages(" |
| + | > # library(emmeans) | ||
| + | > # Calculate estimated marginal means for each group | ||
| + | > gmeans <- emmeans(m.ellmealcat, | ||
| + | > # Perform pairwise comparisons using Tukey' | ||
| + | > pairs(gmeans, | ||
| + | | ||
| + | to46 - to80 | ||
| + | to46 - to100 230.7 12.30 396 18.774 | ||
| + | to80 - to100 | ||
| - | <code> | + | P value adjustment: tukey method for comparing a family of 3 estimates |
| - | FILTER BY filt. | + | > |
| - | regress | + | > m.ellxmealcat |
| - | /dep = api00 | + | > summary(m.ellxmealcat) |
| - | / | + | |
| + | Call: | ||
| + | lm(formula = api00 ~ ell * mealcat, data = df) | ||
| - | Model Summary | + | Residuals: |
| - | Model R R Square Adjusted R Square Std. Error of the Estimate | + | Min |
| - | 1 .648a .420 .413 75.773 | + | -221.854 -43.244 1.437 47.443 181.471 |
| - | a. Predictors: (Constant), parent some college | + | |
| - | ANOVA(b) | + | Coefficients: |
| - | Model Sum of Squares df Mean Square F Sig. | + | Estimate Std. Error t value Pr(>|t|) |
| - | 1 Regression 373644.064 1 373644.064 65.078 .000a | + | (Intercept) |
| - | Residual 516734.838 90 5741.498 | + | ell -3.4447 0.7508 -4.588 6.03e-06 *** |
| - | Total 890378.902 91 | + | mealcatto80 |
| - | a. Predictors: (Constant), parent some college | + | mealcatto100 |
| - | b. Dependent Variable: api 2000 | + | ell: |
| + | ell:mealcatto100 | ||
| + | --- | ||
| + | Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 | ||
| - | Coefficients(a) | + | Residual standard error: 65.47 on 394 degrees of freedom |
| - | Unstandardized Coefficients Standardized Coefficients | + | Multiple R-squared: |
| - | Model B Std. Error Beta t Sig. | + | F-statistic: |
| - | 1 (Constant) 407.039 16.515 24.647 .000 | + | |
| - | parent some college 7.403 .918 .648 8.067 .000 | + | > df %>% |
| - | a. Dependent Variable: api 2000 | + | + |
| + | + | ||
| + | + | ||
| + | + | ||
| + | + | ||
| + | + x = " (ell, english language learner)", | ||
| + | + y = " | ||
| + | `geom_smooth()` using formula = 'y ~ x' | ||
| + | > | ||
| + | > mealcat1 <- summary(m.ellxmealcat)$coefficients[[1]] | ||
| + | > mealcat2 <- summary(m.ellxmealcat)$coefficients[[1]] + | ||
| + | + | ||
| + | > mealcat3 <- summary(m.ellxmealcat)$coefficients[[1]] + | ||
| + | + | ||
| + | > data.frame(mealcat1, | ||
| + | mealcat1 mealcat2 mealcat3 | ||
| + | 1 836.7724 690.1597 565.9368 | ||
| + | > | ||
| + | > ell.slope.mealcat1 <- summary(m.ellxmealcat)$coefficients[[2]] | ||
| + | > ell.slope.mealcat2 <- summary(m.ellxmealcat)$coefficients[[2]] + | ||
| + | + | ||
| + | > ell.slope.mealcat3 <- summary(m.ellxmealcat)$coefficients[[2]] + | ||
| + | + | ||
| + | > data.frame(ell.slope.mealcat1, | ||
| + | + ell.slope.mealcat2, | ||
| + | + ell.slope.mealcat3) | ||
| + | ell.slope.mealcat1 ell.slope.mealcat2 ell.slope.mealcat3 | ||
| + | 1 -3.444691 | ||
| + | > | ||
| </ | </ | ||
| + | </ | ||
| + | {{pasted: | ||
| - | {{regression-cat1.jpg}} | + | ===== Regression with two Catogorical IVs ===== |
| + | <tabbox rs.07> | ||
| + | < | ||
| + | ########################################## | ||
| + | # 카테고리 iv 2개 | ||
| + | ########################################## | ||
| + | m.yrrndmealcat <- lm(api00~yr_rnd+mealcat, | ||
| + | summary(m.yrrndmealcat) | ||
| - | ===== interaction effect ===== | + | # 해석. |
| + | coefs <- summary(m.yrrndmealcat)$coefficients | ||
| + | coefs | ||
| + | br.to46 <- coefs[1] | ||
| + | br.to80 <- coefs[1]+coefs[3] | ||
| + | br.to100 <- coefs[1]+coefs[4] | ||
| + | nobr.to46 <- coefs[1]+coefs[2] | ||
| + | nobr.to80 <- coefs[1]+coefs[2]+coefs[3] | ||
| + | nobr.to100 <- coefs[1]+coefs[2]+coefs[4] | ||
| + | cat(br.to46, | ||
| + | cat(nobr.to46, | ||
| - | 위의 두 regression은 yr_rnd 변인이 갖는 두 가지 특성에 대해서 따로 regression (api_00 | + | # 해석. interaction |
| + | m.yrrndxmealcat | ||
| + | summary(m.yrrndxmealcat) | ||
| - | 아래는 새로운 변인을 만들어서 변인의 값으로 yr_rnd와 some_col값을 곱한 값을 대체한 것이다. 즉, | + | coefs <- summary(m.yrrndxmealcat)$coefficients |
| + | coefs | ||
| + | br.to46 <- coefs[1] | ||
| + | br.to80 <- coefs[1]+coefs[3] | ||
| + | br.to100 <- coefs[1]+coefs[4] | ||
| + | nobr.to46 <- coefs[1]+coefs[2] | ||
| + | nobr.to80 <- coefs[1]+coefs[2]+coefs[3]+coefs[5] | ||
| + | nobr.to100 <- coefs[1]+coefs[2]+coefs[4]+coefs[6] | ||
| + | cat(br.to46, br.to80, br.to100) | ||
| + | cat(nobr.to46, | ||
| + | </ | ||
| + | <tabbox ro.07> | ||
| + | < | ||
| + | > ########################################## | ||
| + | > # 카테고리 iv 2개 | ||
| + | > ########################################## | ||
| + | > m.yrrndmealcat <- lm(api00~yr_rnd+mealcat, | ||
| + | > summary(m.yrrndmealcat) | ||
| - | DV: api00 | + | Call: |
| + | lm(formula = api00 ~ yr_rnd + mealcat, data = df) | ||
| - | IV1: some_col | + | Residuals: |
| - | IV2: yr_rnd | + | |
| - | IV3: yr_rnd * some_col = interaction effects | + | -215.32 |
| - | <code>compute yrXsome = yr_rnd*some_col. | + | Coefficients: |
| - | execute. | + | |
| + | (Intercept) | ||
| + | yr_rndnobr | ||
| + | mealcatto80 | ||
| + | mealcatto100 -281.683 | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 68.89 on 396 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > | ||
| + | > # 해석. | ||
| + | > coefs <- summary(m.yrrndmealcat)$coefficients | ||
| + | > coefs | ||
| + | | ||
| + | (Intercept) | ||
| + | yr_rndnobr | ||
| + | mealcatto80 | ||
| + | mealcatto100 -281.68318 | ||
| + | > br.to46 <- coefs[1] | ||
| + | > br.to80 <- coefs[1]+coefs[3] | ||
| + | > br.to100 <- coefs[1]+coefs[4] | ||
| + | > nobr.to46 <- coefs[1]+coefs[2] | ||
| + | > nobr.to80 <- coefs[1]+coefs[2]+coefs[3] | ||
| + | > nobr.to100 <- coefs[1]+coefs[2]+coefs[4] | ||
| + | > cat(br.to46, | ||
| + | 808.0131 644.2758 526.33 | ||
| + | > cat(nobr.to46, | ||
| + | 765.0531 601.3157 483.3699> | ||
| </ | </ | ||
| + | 예측식은 아래와 같다. | ||
| + | < | ||
| + | y hat = 808.013 + -42.960*(yr_rndno_break) + -163.737(mealcat47-80) + -281.683(mealcat81-100) | ||
| - | 그리고, 이 변인을 regression 공식에 이용한다. | + | yr_rnd: |
| + | break = 방학있음 | ||
| + | no_break = 방학없음 | ||
| - | < | + | mealcat: |
| - | /dep = api00 | + | 0-46% free meals |
| - | /method = enter some_col yr_rnd yrXsome | + | 47-80% |
| - | /save pre. | + | 81-100% |
| </ | </ | ||
| + | 이에 대한 해석은 각각의 독립변인의 종류 수인 2개와 3개를 곱한 6개의 경우로 나누어서 생각할 수 있다. 즉, | ||
| + | '' | ||
| + | 을 바탕으로 각각의 조건을 고려하여 y hat를 계산하면 아래와 같다. | ||
| - | <code>output: | + | <wrap # |
| - | Model Summary(b) | + | TABLE. Two dummy variables |
| - | Model R R Square Adjusted R Square Std. Error of the Estimate | + | ^ ^ mealcat0-46 |
| - | 1 .532a .283 .277 120.922 | + | | yr_rndbreak |
| - | a. Predictors: (Constant), yrXsome, parent some college, year round school | + | yr_rndno_break = 0 |
| - | b. Dependent Variable: api 2000 | + | mealcat0-46 = 1 |
| + | mealcat47-80 = 0 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | </ | ||
| + | yr_rndno_break = 0 | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat47-80 = 1 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | </ | ||
| + | yr_rndno_break = 0 | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat47-80 = 0 | ||
| + | mealcat81-100 = 1 경우 | ||
| + | '' | ||
| + | </ | ||
| + | | yr_rndno_break | ||
| + | yr_rndno_break = 1 | ||
| + | mealcat0-46 = 1 | ||
| + | mealcat47-80 = 0 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | </ | ||
| + | yr_rndno_break = 1 | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat47-80 = 1 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | </ | ||
| + | yr_rndno_break = 1 | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat47-80 = 0 | ||
| + | mealcat81-100 = 1 경우 | ||
| + | '' | ||
| + | </ | ||
| - | ANOVA(b) | + | < |
| - | Model Sum of Squares df Mean Square F Sig. | + | > # 해석. interaction |
| - | 1 Regression 2283345.485 3 761115.162 52.053 .000a | + | > m.yrrndxmealcat <- lm(api00~yr_rnd*mealcat, |
| - | Residual 5790326.513 396 14622.037 | + | > summary(m.yrrndxmealcat) |
| - | Total 8073671.997 399 | + | |
| - | a. Predictors: | + | |
| - | b. Dependent Variable: api 2000 | + | |
| - | Coefficients(a) | + | Call: |
| - | Unstandardized Coefficients Standardized Coefficients | + | lm(formula = api00 ~ yr_rnd * mealcat, data = df) |
| - | Model B Std. Error Beta t Sig. | + | |
| - | 1 (Constant) 655.110 14.035 46.677 .000 | + | |
| - | parent some college 1.409 .586 .112 2.407 .017 | + | |
| - | year round school -248.071 29.859 -.735 -8.308 .000 | + | |
| - | __yrXsome__ 5.993 1.577 .330 3.800 .000 | + | |
| - | a. Dependent Variable: api 2000 | + | |
| - | Residuals | + | Residuals: |
| - | Minimum Maximum Mean Std. Deviation N | + | |
| - | Predicted Value 407.04 749.54 647.62 75.648 400 | + | -207.533 |
| - | Residual -275.118 279.252 .000 120.466 400 | + | |
| - | Std. Predicted Value -3.180 1.347 .000 1.000 400 | + | Coefficients: |
| - | Std. Residual -2.275 2.309 .000 .996 400 | + | Estimate Std. Error t value Pr(>|t|) |
| - | a. Dependent Variable: api 2000 | + | (Intercept) |
| + | yr_rndnobr | ||
| + | mealcatto80 | ||
| + | mealcatto100 | ||
| + | yr_rndnobr: | ||
| + | yr_rndnobr: | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > | ||
| + | > coefs <- summary(m.yrrndxmealcat)$coefficients | ||
| + | > coefs | ||
| + | Estimate | ||
| + | (Intercept) | ||
| + | yr_rndnobr | ||
| + | mealcatto80 | ||
| + | mealcatto100 | ||
| + | yr_rndnobr: | ||
| + | yr_rndnobr: | ||
| + | > br.to46 <- coefs[1] | ||
| + | > br.to80 <- coefs[1]+coefs[3] | ||
| + | > br.to100 <- coefs[1]+coefs[4] | ||
| + | > nobr.to46 <- coefs[1]+coefs[2] | ||
| + | > nobr.to80 <- coefs[1]+coefs[2]+coefs[3]+coefs[5] | ||
| + | > nobr.to100 <- coefs[1]+coefs[2]+coefs[4]+coefs[6] | ||
| + | > cat(br.to46, br.to80, br.to100) | ||
| + | 809.6855 645.2735 521.4925 | ||
| + | > cat(nobr.to46, | ||
| + | 735.4286 593.5333 488 | ||
| + | > | ||
| </ | </ | ||
| + | 위의 테스트는 두 개의 독립변인이 모두 종류이고 종속변인이 숫자일 때의 조건을 만족하니 factorial anova를 해도 된다. 아래는 그 결과이다. | ||
| + | < | ||
| + | > mod4 <- lm(api00 ~ yr_rnd + mealcat + yr_rnd: | ||
| + | > summary(mod4) | ||
| - | 위에서 __yr_rnd__의 b 계수 값이 5.993으로 유의미하다고 판단된다 | + | Call: |
| + | lm(formula | ||
| - | {{regression-cat2-interctionx.jpg}} | + | Residuals: |
| - | {{regression-cat2-interaction-each.jpg}} | + | |
| + | -207.533 | ||
| - | 위의 테스트를 살펴보면, | + | Coefficients: |
| + | | ||
| + | (Intercept) | ||
| + | yr_rndno_break | ||
| + | mealcat47-80 | ||
| + | mealcat81-100 | ||
| + | yr_rndno_break: | ||
| + | yr_rndno_break: | ||
| + | --- | ||
| + | Signif. codes: | ||
| - | < | + | Residual standard error: 68.87 on 394 degrees of freedom |
| - | | + | Multiple R-squared: |
| - | | + | F-statistic: |
| + | </ | ||
| - | or | + | < |
| - | UNIANOVA api00 BY yr_rnd WITH some_col | + | Coefficients: |
| - | | + | Estimate Std. Error t value Pr(> |
| - | | + | (Intercept) |
| + | yr_rndno_break | ||
| + | mealcat47-80 | ||
| + | mealcat81-100 | ||
| + | yr_rndno_break: | ||
| + | yr_rndno_break: | ||
| + | --- | ||
| + | 이전 식 | ||
| + | y hat = 808.013 + -42.960 * (nobr) + -163.737 * (to80) + -281.683 * (to100) | ||
| + | 위의 식 | ||
| + | y hat = 809.685 + -74.257*(nobr) + | ||
| + | -164.412*(to80) + | ||
| + | -288.193*(to100) + | ||
| + | | ||
| + | | ||
| + | |||
| + | yr_rnd: | ||
| + | break = 방학있음 | ||
| + | no_break = 방학없음 | ||
| + | |||
| + | mealcat: | ||
| + | 0-46% free meals | ||
| + | 47-80% | ||
| + | 81-100% | ||
| </ | </ | ||
| + | |||
| + | ^ ^ mealcat0-46 | ||
| + | | yr_rndbreak | ||
| + | yr_rndno_break = 0 | ||
| + | mealcat47-80 = 0 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | </ | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | 163.737 | ||
| + | = 645.9'' | ||
| + | </ | ||
| + | yr_rndno_break = 0 | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat47-80 = 0 경우 | ||
| + | '' | ||
| + | 281.683 | ||
| + | = 528'' | ||
| + | </ | ||
| + | | yr_rndno_break | ||
| + | yr_rndbreak = 0 | ||
| + | mealcat47-80 = 0 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | 74.257 | ||
| + | = 735.4'' | ||
| + | </ | ||
| + | yr_rndbreak = 0 | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat81-100 = 0 경우 | ||
| + | '' | ||
| + | 74.257 - | ||
| + | 164.412 + | ||
| + | <fc # | ||
| + | = 593.5'' | ||
| + | </ | ||
| + | yr_rndbreak = 0 | ||
| + | mealcat0-46 = 0 | ||
| + | mealcat47-80 = 0 경우 | ||
| + | '' | ||
| + | 74.257 - | ||
| + | 288.193 + | ||
| + | <fc # | ||
| + | = 488'' | ||
| + | </ | ||
| + | 마지막 두 케이스를 보면 no_break학교 중에서 밀카테고리 2와 3에서 떨어지는 정도가 어느 정도 완화되는 경향을 보이지만 통계학적으로 significant하지는 않다. | ||
| + | </ | ||
using_dummy_variables.1781044627.txt.gz · Last modified: by hkimscil
