r:dummy_variables_with_significant_interaction
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
r:dummy_variables_with_significant_interaction [2023/06/02 01:23] – created hkimscil | r:dummy_variables_with_significant_interaction [2023/06/05 22:01] (current) – [Regression with a continous + a categorical variables: ANCOVA] hkimscil | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Regression with two categorical variables (IVs): Two way anova ====== | ||
+ | https:// | ||
< | < | ||
college <- read.csv(" | college <- read.csv(" | ||
Line 12: | Line 14: | ||
m1 <- lm(salary~public+location) | m1 <- lm(salary~public+location) | ||
m2 <- lm(salary~public*location) | m2 <- lm(salary~public*location) | ||
+ | |||
summary(m1) | summary(m1) | ||
summary(m2) | summary(m2) | ||
+ | a.m2 <- aov(salary~public*location) | ||
+ | summary(a.m2) | ||
+ | |||
+ | interaction.plot(x.factor = location, | ||
+ | | ||
+ | | ||
+ | fun = median, | ||
+ | ylab = " | ||
+ | xlab = " | ||
+ | | ||
+ | lty = 1, | ||
+ | | ||
+ | | ||
</ | </ | ||
+ | ====== output ====== | ||
+ | < | ||
+ | #################### | ||
+ | > college <- read.csv(" | ||
+ | > attach(college) | ||
+ | The following object is masked from acne.re (pos = 36): | ||
+ | |||
+ | id | ||
+ | |||
+ | The following object is masked from acne.re (pos = 37): | ||
+ | |||
+ | id | ||
+ | |||
+ | > str(college) | ||
+ | ' | ||
+ | $ id : int 1 2 3 4 5 6 7 8 9 10 ... | ||
+ | $ name : chr " | ||
+ | $ salary | ||
+ | $ cost : int 189300 189600 188400 188700 194200 181900 191300 187600 180400 184900 ... | ||
+ | $ public | ||
+ | $ location: int 3 3 3 3 3 2 3 1 3 3 ... | ||
+ | > head(college) | ||
+ | id name salary | ||
+ | 1 1 Massachusetts Institute of Technology (MIT) 119000 189300 | ||
+ | 2 2 Harvard University 121000 189600 | ||
+ | 3 3 | ||
+ | 4 4 Princeton University 123000 188700 | ||
+ | 5 5 Yale University 110000 194200 | ||
+ | 6 6 University of Notre Dame 112000 181900 | ||
+ | > salary <- salary / 1000 | ||
+ | > public< | ||
+ | > location< | ||
+ | > m1 <- lm(salary~public+location) | ||
+ | > m2 <- lm(salary~public*location) | ||
+ | > summary(m1) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = salary ~ public + location) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -17.15 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) | ||
+ | publicPublic | ||
+ | locationMW | ||
+ | locationNE | ||
+ | locationW | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 7.86 on 80 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > summary(m2) | ||
+ | |||
+ | Call: | ||
+ | lm(formula = salary ~ public * location) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -11.19 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | publicPublic | ||
+ | locationMW | ||
+ | locationNE | ||
+ | locationW | ||
+ | publicPublic: | ||
+ | publicPublic: | ||
+ | publicPublic: | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 6.95 on 77 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | > | ||
+ | > | ||
+ | > a.m2 <- aov(salary~public*location) | ||
+ | > summary(a.m2) | ||
+ | Df Sum Sq Mean Sq F value Pr(> | ||
+ | public | ||
+ | location | ||
+ | public: | ||
+ | Residuals | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | > | ||
+ | > | ||
+ | > interaction.plot(x.factor = location, | ||
+ | + trace.factor = public, | ||
+ | + response = salary, | ||
+ | + fun = median, | ||
+ | + ylab = " | ||
+ | + xlab = " | ||
+ | + col=c(" | ||
+ | + lty = 1, | ||
+ | + lwd=2, | ||
+ | + trace.label=" | ||
+ | > | ||
+ | </ | ||
+ | {{: | ||
+ | |||
+ | ====== Regression with a continous + a categorical variables: ANCOVA ====== | ||
+ | < | ||
+ | summary(mod< | ||
+ | anova(mod) | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > summary(mod< | ||
+ | |||
+ | Call: | ||
+ | lm(formula = salary ~ cost + location) | ||
+ | |||
+ | Residuals: | ||
+ | Min 1Q Median | ||
+ | -20.244 | ||
+ | |||
+ | Coefficients: | ||
+ | | ||
+ | (Intercept) | ||
+ | cost | ||
+ | locationMW | ||
+ | locationNE | ||
+ | locationW | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 8.02 on 80 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > anova(mod) | ||
+ | Analysis of Variance Table | ||
+ | |||
+ | Response: salary | ||
+ | Df Sum Sq Mean Sq F value Pr(> | ||
+ | cost | ||
+ | location | ||
+ | Residuals 80 | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | > | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | (Intercept) | ||
+ | cost | ||
+ | locationMW | ||
+ | locationNE | ||
+ | locationW | ||
+ | |||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | * S, MW, NE, W 중에서 S가 default | ||
+ | * y hat ~ 87.8 + .00006*cost | ||
+ | * MW: | ||
+ | * y hat ~ 87.8 - 2.8 + .00006*cost | ||
+ | * y hat ~ 85 + .00006*cost | ||
+ | * NE: | ||
+ | * y hat ~ 87.8 - 9.23 + .00006*cost | ||
+ | * y hat ~ 78.57 + .00006*cost | ||
+ | * E: | ||
+ | * y hat ~ 87.8 - 10.5 + .00006*cost | ||
+ | * y hat ~ 77.3 + .00006*cost | ||
+ | |||
+ | < | ||
+ | > summary(mod2< | ||
+ | |||
+ | Call: | ||
+ | lm(formula = salary ~ cost * location) | ||
+ | |||
+ | Residuals: | ||
+ | | ||
+ | -17.1126 | ||
+ | |||
+ | Coefficients: | ||
+ | Estimate Std. Error t value Pr(> | ||
+ | (Intercept) | ||
+ | cost | ||
+ | locationMW | ||
+ | locationNE | ||
+ | locationW | ||
+ | cost: | ||
+ | cost: | ||
+ | cost: | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | |||
+ | Residual standard error: 7.644 on 77 degrees of freedom | ||
+ | Multiple R-squared: | ||
+ | F-statistic: | ||
+ | |||
+ | > anova(mod2) | ||
+ | Analysis of Variance Table | ||
+ | |||
+ | Response: salary | ||
+ | Df Sum Sq Mean Sq F value Pr(> | ||
+ | cost 1 2757.0 2756.97 | ||
+ | location | ||
+ | cost: | ||
+ | Residuals | ||
+ | --- | ||
+ | Signif. codes: | ||
+ | > | ||
+ | </ | ||
+ | |||
+ |
r/dummy_variables_with_significant_interaction.1685636580.txt.gz · Last modified: 2023/06/02 01:23 by hkimscil