#Setup

library(ggplot2)
library(ggpubr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ✔ purrr   0.3.5      
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(effectsize)
library(tidyr)
library(rstatix)
## 
## Attaching package: 'rstatix'
## 
## The following objects are masked from 'package:effectsize':
## 
##     cohens_d, eta_squared
## 
## The following object is masked from 'package:stats':
## 
##     filter
library(dplyr)

#read in data

MyData <- read.csv("~/Documents/Fall 2022/PSY 211/2014-2022 ONLY.csv")

#read in my data set, remove missing data coded as 99 and create new data set

WSself <- dplyr::select(MyData, PUBPRIV, PROGRAM, C.E, RACE, ROSPST, LEVEL)

WSselfGrades <-na_if (WSself, "99")
WSselfGrades <- na.omit(WSselfGrades)
PUBPRIVgroups <- cut(WSselfGrades$PUBPRIV, breaks = c(0,1,2), labels = c("Public","Private"))
WSselfGrades$PUBPRIV[1:20]
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
PUBPRIVgroups[1:20]
##  [1] Public Public Public Public Public Public Public Public Public Public
## [11] Public Public Public Public Public Public Public Public Public Public
## Levels: Public Private
WSselfGrades$PUBPRIVgroups <- as.factor(WSselfGrades$PUBPRIV)
class(WSselfGrades$PUBPRIVgroups)
## [1] "factor"
levels(WSselfGrades$PUBPRIVgroups) <-c("Public", "Private")
C.Egroups <- cut(WSselfGrades$C.E, breaks = c(0,1,2), labels = c("Comparsion","Experimental"))
WSselfGrades$C.E[1:20]
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
C.Egroups[1:20]
##  [1] Comparsion Comparsion Comparsion Comparsion Comparsion Comparsion
##  [7] Comparsion Comparsion Comparsion Comparsion Comparsion Comparsion
## [13] Comparsion Comparsion Comparsion Comparsion Comparsion Comparsion
## [19] Comparsion Comparsion
## Levels: Comparsion Experimental
WSselfGrades$C.Egroups <- as.factor(WSselfGrades$C.E)
class(WSselfGrades$C.Egroups)
## [1] "factor"
levels(WSselfGrades$C.Egroups) <-c("Comparsion", "Experimental")
ggplot(WSselfGrades, aes(fill=PUBPRIVgroups, y=ROSPST, x=C.Egroups)) + 
    geom_bar(position="dodge", stat="identity")+
  xlab("Program Exposure")+
  ylab("POST Self Esteem Scores")

#cell means

cellDescript <- with(WSselfGrades, aggregate(x=list(Mean=ROSPST,SD=ROSPST),
                                  by=list(F1=PUBPRIVgroups, F2=C.Egroups),
                                  FUN=mean_sd) )
View(cellDescript)
## Warning in format.data.frame(x0): corrupt data frame: columns will be truncated
## or padded with NAs

running ANOVA

model1 <- aov(ROSPST~PUBPRIVgroups*C.Egroups,data=WSselfGrades)

summary(model1)
##                          Df Sum Sq Mean Sq F value   Pr(>F)    
## PUBPRIVgroups             1    364   363.8   11.34 0.000788 ***
## C.Egroups                 1   1556  1555.7   48.50 6.16e-12 ***
## PUBPRIVgroups:C.Egroups   1      7     6.7    0.21 0.647140    
## Residuals               952  30537    32.1                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
simpEff <- WSselfGrades %>% filter(C.Egroups=="Comparsion") %>% 
  aov(WSselfGrades$ROSPST~WSselfGrades$PUBPRIVgroups,data=.) 

summary(simpEff)
##                             Df Sum Sq Mean Sq F value  Pr(>F)   
## WSselfGrades$PUBPRIVgroups   1    364   363.8   10.81 0.00104 **
## Residuals                  954  32100    33.6                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(simpEff)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = WSselfGrades$ROSPST ~ WSselfGrades$PUBPRIVgroups, data = .)
## 
## $`WSselfGrades$PUBPRIVgroups`
##                    diff      lwr      upr     p adj
## Private-Public 1.269158 0.511734 2.026582 0.0010447
simpEff2 <- WSselfGrades %>% filter(WSselfGrades$PUBPRIVgroups=="Public") %>% 
  aov(WSselfGrades$ROSPST~WSselfGrades$C.Egroups,data=.) 

summary(simpEff2)
##                         Df Sum Sq Mean Sq F value   Pr(>F)    
## WSselfGrades$C.Egroups   1   1559  1559.3   48.13 7.34e-12 ***
## Residuals              954  30904    32.4                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
WSselfGrades %>% filter(WSselfGrades$PUBPRIVgroups=="Public") %>% 
  t.test(WSselfGrades$ROSPST~WSselfGrades$PUBPRIVgroups,data=.) 
## 
##  Welch Two Sample t-test
## 
## data:  WSselfGrades$ROSPST by WSselfGrades$PUBPRIVgroups
## t = -3.388, df = 847.17, p-value = 0.0007366
## alternative hypothesis: true difference in means between group Public and group Private is not equal to 0
## 95 percent confidence interval:
##  -2.004426 -0.533890
## sample estimates:
##  mean in group Public mean in group Private 
##              20.09016              21.35932
library(lsr)
WSselfGrades %>% filter(WSselfGrades$PUBPRIVgroups=="Public") %>% 
  cohensD(WSselfGrades$ROSPST~WSselfGrades$PUBPRIVgroups,data=.) 
## [1] 0.2187955
WSselfGrades %>% filter(WSselfGrades$C.Egroups=="Experimental") %>% 
  t.test(WSselfGrades$ROSPST~WSselfGrades$C.Egroups,data=.) 
## 
##  Welch Two Sample t-test
## 
## data:  WSselfGrades$ROSPST by WSselfGrades$C.Egroups
## t = -6.9325, df = 948.03, p-value = 7.64e-12
## alternative hypothesis: true difference in means between group Comparsion and group Experimental is not equal to 0
## 95 percent confidence interval:
##  -3.277917 -1.831517
## sample estimates:
##   mean in group Comparsion mean in group Experimental 
##                   19.62012                   22.17484
library(lsr)
WSselfGrades %>% filter(WSselfGrades$C.Egroups=="Experimental") %>% 
  cohensD(WSselfGrades$ROSPST~WSselfGrades$C.Egroups,data=.) 
## [1] 0.4488559

#ANOVA effect size

partial_eta_squared(model1)
##           PUBPRIVgroups               C.Egroups PUBPRIVgroups:C.Egroups 
##            0.0117741803            0.0484750302            0.0002201801
library(report)
report(model1)
## Warning: Could not find Sum-of-Squares for the (Intercept) in the ANOVA table.
## The ANOVA (formula: ROSPST ~ PUBPRIVgroups * C.Egroups) suggests that:
## 
##   - The main effect of PUBPRIVgroups is statistically significant and small (F(1,
## 952) = 11.34, p < .001; Eta2 (partial) = 0.01, 95% CI [3.09e-03, 1.00])
##   - The main effect of C.Egroups is statistically significant and small (F(1,
## 952) = 48.50, p < .001; Eta2 (partial) = 0.05, 95% CI [0.03, 1.00])
##   - The interaction between PUBPRIVgroups and C.Egroups is statistically not
## significant and very small (F(1, 952) = 0.21, p = 0.647; Eta2 (partial) =
## 2.20e-04, 95% CI [0.00, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.
report(simpEff)
## Warning: Could not find Sum-of-Squares for the (Intercept) in the ANOVA table.
## Warning: Using `$` in model formulas can produce unexpected results. Specify your
##   model using the `data` argument instead.
##   Try: ROSPST ~ PUBPRIVgroups,
##   data = WSselfGrades
## The ANOVA (formula: WSselfGrades$ROSPST ~ WSselfGrades$PUBPRIVgroups) suggests
## that:
## 
##   - The main effect of WSselfGrades$PUBPRIVgroups is statistically significant
## and small (F(1, 954) = 10.81, p = 0.001; Eta2 = 0.01, 95% CI [2.80e-03, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.