#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=WSselfGrades$ROSPST,SD=WSselfGrades$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

#ANOVA effect size

partial_eta_squared(model1)
##           PUBPRIVgroups               C.Egroups PUBPRIVgroups:C.Egroups 
##            0.0117741803            0.0484750302            0.0002201801

#mean and SD for each group

WSselfGrades %>%
  group_by(PUBPRIVgroups) %>%
  get_summary_stats(ROSPST, type = "mean_sd")
## # A tibble: 2 × 5
##   PUBPRIVgroups variable     n  mean    sd
##   <fct>         <fct>    <dbl> <dbl> <dbl>
## 1 Public        ROSPST     366  20.1  5.34
## 2 Private       ROSPST     590  21.4  6.07
WSselfGrades %>%
  group_by(C.Egroups) %>%
  get_summary_stats(ROSPST, type = "mean_sd")
## # A tibble: 2 × 5
##   C.Egroups    variable     n  mean    sd
##   <fct>        <fct>    <dbl> <dbl> <dbl>
## 1 Comparsion   ROSPST     487  19.6  5.57
## 2 Experimental ROSPST     469  22.2  5.81

#Refercenes

citation("effectsize")
## 
## To cite effectsize in publications use:
## 
##   Ben-Shachar M, Lüdecke D, Makowski D (2020). effectsize: Estimation
##   of Effect Size Indices and Standardized Parameters. Journal of Open
##   Source Software, 5(56), 2815. doi: 10.21105/joss.02815
## 
## A BibTeX entry for LaTeX users is
## 
##   @Article{,
##     title = {{e}ffectsize: Estimation of Effect Size Indices and Standardized Parameters},
##     author = {Mattan S. Ben-Shachar and Daniel Lüdecke and Dominique Makowski},
##     year = {2020},
##     journal = {Journal of Open Source Software},
##     volume = {5},
##     number = {56},
##     pages = {2815},
##     publisher = {The Open Journal},
##     doi = {10.21105/joss.02815},
##     url = {https://doi.org/10.21105/joss.02815},
##   }

citation(“dplyr”)

library(ggpubr)

library(tidyr) library(rstatix) library(dplyr) ```