psych=read.csv("/Users/nupoor/Documents/MSDA/DA- Algo/Week 2 exercise/psych.csv", header = TRUE)Psych
install.packages(“car”); install.packages(“DescTools”); install.packages(“MASS”) library(DescTools); library(MASS); library(car); library(hms)
psych$sex= as.factor(psych$sex)
psych$rank= as.factor(psych$rank)str(psych)'data.frame': 22 obs. of 3 variables:
$ sex : Factor w/ 2 levels "F","M": 1 1 1 1 1 1 2 2 2 2 ...
$ rank : Factor w/ 2 levels "Assist","Assoc": 1 1 1 1 1 1 1 1 1 1 ...
$ salary: int 33 36 35 38 42 37 39 38 40 44 ...
table(psych$sex)
F M
10 12
table(psych$rank)
Assist Assoc
10 12
# type 1 order 1
aov.psych1 <- aov(salary ~ sex * rank , data = psych)
summary(aov.psych1) Df Sum Sq Mean Sq F value Pr(>F)
sex 1 155.15 155.15 17.007 0.000637 ***
rank 1 169.82 169.82 18.616 0.000417 ***
sex:rank 1 0.63 0.63 0.069 0.795101
Residuals 18 164.21 9.12
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# type 1 order 2
lm.psych2 <- lm(salary ~ rank * sex, data = psych)
car::Anova(lm.psych2, type = "II")Anova Table (Type II tests)
Response: salary
Sum Sq Df F value Pr(>F)
rank 169.825 1 18.6156 0.0004174 ***
sex 72.758 1 7.9755 0.0112406 *
rank:sex 0.634 1 0.0695 0.7951007
Residuals 164.208 18
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#type 3 order 1
lm.psych2 <- lm(salary ~ sex * rank , data = psych)
car::Anova(lm.psych2,type=3)Anova Table (Type III tests)
Response: salary
Sum Sq Df F value Pr(>F)
(Intercept) 8140.2 1 892.2994 < 2e-16 ***
sex 28.0 1 3.0711 0.09671 .
rank 70.4 1 7.7189 0.01240 *
sex:rank 0.6 1 0.0695 0.79510
Residuals 164.2 18
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#type 3 order 2
lm.psych2 <- aov(salary ~ rank * sex , data = psych)
car::Anova(lm.psych2, type=3)Anova Table (Type III tests)
Response: salary
Sum Sq Df F value Pr(>F)
(Intercept) 8140.2 1 892.2994 < 2e-16 ***
rank 70.4 1 7.7189 0.01240 *
sex 28.0 1 3.0711 0.09671 .
rank:sex 0.6 1 0.0695 0.79510
Residuals 164.2 18
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
lm.psych= lm(salary ~ sex * rank , data = psych)
anova(lm.psych)Analysis of Variance Table
Response: salary
Df Sum Sq Mean Sq F value Pr(>F)
sex 1 155.152 155.152 17.0072 0.0006373 ***
rank 1 169.825 169.825 18.6156 0.0004174 ***
sex:rank 1 0.634 0.634 0.0695 0.7951007
Residuals 18 164.208 9.123
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(lm.psych)$r.squared[1] 0.6647566
#type 1 order 1 without interaction
aov.psych3 <- aov(salary ~ rank + sex , data = psych)
summary(aov.psych3) Df Sum Sq Mean Sq F value Pr(>F)
rank 1 252.22 252.22 29.071 3.34e-05 ***
sex 1 72.76 72.76 8.386 0.00926 **
Residuals 19 164.84 8.68
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#type 1 order 2 without interaction
aov.psych4 <- aov(salary ~ sex + rank , data = psych)
summary(aov.psych4) Df Sum Sq Mean Sq F value Pr(>F)
sex 1 155.2 155.15 17.88 0.000454 ***
rank 1 169.8 169.82 19.57 0.000291 ***
Residuals 19 164.8 8.68
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#type 3 order 1 without interaction
lm.psych3 <- lm(salary ~ rank + sex , data = psych)
car::Anova(lm.psych3,type=3)Anova Table (Type III tests)
Response: salary
Sum Sq Df F value Pr(>F)
(Intercept) 10227.6 1 1178.8469 < 2.2e-16 ***
rank 169.8 1 19.5743 0.0002912 ***
sex 72.8 1 8.3862 0.0092618 **
Residuals 164.8 19
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#type 3 order 2
lm.psych4 <- aov(salary ~ sex + rank , data = psych)
car::Anova(lm.psych4,type=3)Anova Table (Type III tests)
Response: salary
Sum Sq Df F value Pr(>F)
(Intercept) 10227.6 1 1178.8469 < 2.2e-16 ***
sex 72.8 1 8.3862 0.0092618 **
rank 169.8 1 19.5743 0.0002912 ***
Residuals 164.8 19
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
lm.psych_type3= lm(salary ~ rank + sex , data = psych)
anova(lm.psych_type3)Analysis of Variance Table
Response: salary
Df Sum Sq Mean Sq F value Pr(>F)
rank 1 252.218 252.218 29.0711 3.339e-05 ***
sex 1 72.758 72.758 8.3862 0.009262 **
Residuals 19 164.842 8.676
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(lm.psych_type3)$r.squared [1] 0.6634627
par(mfrow=c(2,2))
plot(aov.psych3)par(mfrow=c(1,1))
plot(aov.psych3, 1)par(mfrow=c(1,1))
plot(aov.psych3, 2)