1 Paquetes

library(boot)
library(ggm)
library(ggplot2)
library(polycor)
library(Hmisc)
library(dplyr)
library(readxl)
library(devtools)
library(tidyverse)
library(lubridate)
library(ggridges)
library(wesanderson)
library(RColorBrewer)
library(knitr)
library(kableExtra)
library(hrbrthemes)
library(statsr)
library(stargazer)
library(psych)
library(corrplot)
library(corrr)
library(GGally)
library(ggcorrplot)
library(PerformanceAnalytics)
library(pander)
library(broom)
library(purrr)
library(kableExtra)
library(egg)

#library(rstatix)
#library(ggpubr)
webshot::install_phantomjs()

2 Alistando los datos

#Working Directory

#setwd("~/Santiago/Clases Camila/Cornell_Tests")

#Creando carpetas para guardar los outputs

dir.create("Correlation_Matrices_html")
dir.create("Summary_Tables")
dir.create("Correlation_Plots")
dir.create("Regression_Tables")
dir.create("Graficas_Definitivas")
dir.create("./Graficas_Definitivas/SVG")
dir.create("./T_Tests")

#Cargando datos

Cornell_niveles <- read_excel("Cornell_niveles.xlsx")

Cornell_literacy <- read_excel("Correlations_CornellLiteracy.xlsx")

3 Manipulación de datos

#Voy a empezar explorando la base Cornell_niveles

head(Cornell_niveles)
## # A tibble: 6 x 10
##   Total_PreCornell Total_PostCornell `Induction pre` `Induction Post`
##              <dbl>             <dbl>           <dbl>            <dbl>
## 1               21                31               3                8
## 2               29                37              15               16
## 3               28                34              13               12
## 4               27                30              16               13
## 5               22                24              11                5
## 6               33                36              11               13
## # ... with 6 more variables: Deduction Pre <dbl>, Deduction Post <dbl>,
## #   Obs Cred Pre <dbl>, Obs Cred Post <dbl>, Assumptions Pre <dbl>,
## #   Assumptions post <dbl>
View(Cornell_literacy)

#Renombrando variables

names(Cornell_niveles)
##  [1] "Total_PreCornell"  "Total_PostCornell" "Induction pre"    
##  [4] "Induction Post"    "Deduction Pre"     "Deduction Post"   
##  [7] "Obs Cred Pre"      "Obs Cred Post"     "Assumptions Pre"  
## [10] "Assumptions post"
View(Cornell_niveles)
View(Cornell_literacy)
Cornell_niveles<- Cornell_niveles %>% 
  rename(Induction_Pre=`Induction pre`,
         Induction_Post=`Induction Post`,
         Deduction_Pre=`Deduction Pre`,
         Deduction_Post=`Deduction Post`,
         Obs_Cred_Pre=`Obs Cred Pre`,
         Obs_Cred_Post=`Obs Cred Post`,
         Assumptions_Pre=`Assumptions Pre`,
         Assumptions_Post=`Assumptions post`)

#La misma base con los nuevos nombres (sin espacios)
names(Cornell_niveles)
##  [1] "Total_PreCornell"  "Total_PostCornell" "Induction_Pre"    
##  [4] "Induction_Post"    "Deduction_Pre"     "Deduction_Post"   
##  [7] "Obs_Cred_Pre"      "Obs_Cred_Post"     "Assumptions_Pre"  
## [10] "Assumptions_Post"
#Para lograr meter todo en una sola gráfica necesitamos una variable
#Categórica que nos indique si el valor del test es pre o post.Eso
#me permite unificar los valores de una misma competencia en una sola columna. 
#para ello tendré que camiar de nuevo los nombres. El nombre de cada columna va
#a ser la competencia como tal. Sin el "pre y el post".



#Creando nueva variable categórica.(TiMe. Dos niveles (Pre y Post))
names(Cornell_niveles)
##  [1] "Total_PreCornell"  "Total_PostCornell" "Induction_Pre"    
##  [4] "Induction_Post"    "Deduction_Pre"     "Deduction_Post"   
##  [7] "Obs_Cred_Pre"      "Obs_Cred_Post"     "Assumptions_Pre"  
## [10] "Assumptions_Post"
#Pre

Cornell_niveles_Pre <- Cornell_niveles %>% 
  select(Total_PreCornell,Induction_Pre,Deduction_Pre,
         Obs_Cred_Pre,Assumptions_Pre) %>% 
  mutate(Time=as.factor("Pre"))

  #Renaming

Cornell_niveles_Pre <- Cornell_niveles_Pre %>% 
    rename(
      Total_Cornell=Total_PreCornell, 
      Induction= Induction_Pre,    
      Deduction=Deduction_Pre,    
      Obs_Cred=Obs_Cred_Pre,   
      Assumptions=Assumptions_Pre  
    )


#Creando variable Skill

Cornell_niveles_Pre_Total <- Cornell_niveles_Pre %>% 
  select(Total_Cornell,Time) %>% 
  mutate(Skill=as.factor("Total")) %>% 
  rename(Score=Total_Cornell)

Cornell_niveles_Pre_Induction <- Cornell_niveles_Pre %>% 
  select(Induction,Time) %>% 
  mutate(Skill=as.factor("Induction")) %>% 
  rename(Score=Induction)

Cornell_niveles_Pre_Deduction <- Cornell_niveles_Pre %>% 
    select(Deduction,Time) %>% 
  mutate(Skill=as.factor("Deduction")) %>% 
  rename(Score=Deduction)

Cornell_niveles_Pre_Obs_Cred <- Cornell_niveles_Pre %>% 
  select(Obs_Cred,Time) %>% 
  mutate(Skill=as.factor("Obs_Cred")) %>% 
  rename(Score=Obs_Cred)

Cornell_niveles_Pre_Assumptions <- Cornell_niveles_Pre %>% 
  select(Assumptions,Time) %>% 
  mutate(Skill=as.factor("Assumptions")) %>% 
  rename(Score=Assumptions)

Base_Pre <- rbind(Cornell_niveles_Pre_Total,
                  Cornell_niveles_Pre_Induction,
                  Cornell_niveles_Pre_Deduction,
                  Cornell_niveles_Pre_Obs_Cred,
                  Cornell_niveles_Pre_Assumptions)

Base_Pre2_SinTotal <- rbind(Cornell_niveles_Pre_Induction,
                  Cornell_niveles_Pre_Deduction,
                  Cornell_niveles_Pre_Obs_Cred,
                  Cornell_niveles_Pre_Assumptions)


#Post

Cornell_niveles_Post <- Cornell_niveles %>% 
  select(Total_PostCornell,Induction_Post,Deduction_Post,
         Obs_Cred_Post,Assumptions_Post) %>% 
  mutate(Time=as.factor("Post"))


  #Renaming

  Cornell_niveles_Post<- Cornell_niveles_Post %>% 
  rename(
    Total_Cornell=Total_PostCornell, 
    Induction= Induction_Post,    
    Deduction=Deduction_Post,    
    Obs_Cred=Obs_Cred_Post,   
    Assumptions=Assumptions_Post  
    )

#Creando variable skill en Post
  
  Cornell_niveles_Post_Total <- Cornell_niveles_Post %>% 
    select(Total_Cornell,Time) %>% 
    mutate(Skill=as.factor("Total")) %>% 
    rename(Score=Total_Cornell)
  
  
  Cornell_niveles_Post_Induction <- Cornell_niveles_Post %>% 
    select(Induction,Time) %>% 
    mutate(Skill=as.factor("Induction")) %>% 
    rename(Score=Induction)
  
  Cornell_niveles_Post_Deduction <- Cornell_niveles_Post %>% 
    select(Deduction,Time) %>% 
    mutate(Skill=as.factor("Deduction")) %>% 
    rename(Score=Deduction)
  
  Cornell_niveles_Post_Obs_Cred <- Cornell_niveles_Post %>% 
    select(Obs_Cred,Time) %>% 
    mutate(Skill=as.factor("Obs_Cred")) %>% 
    rename(Score=Obs_Cred)
  
  Cornell_niveles_Post_Assumptions <- Cornell_niveles_Post %>% 
    select(Assumptions,Time) %>% 
    mutate(Skill=as.factor("Assumptions")) %>% 
    rename(Score=Assumptions)
  
  Base_Post <- rbind(Cornell_niveles_Post_Total,
                    Cornell_niveles_Post_Induction,
                    Cornell_niveles_Post_Deduction,
                    Cornell_niveles_Post_Obs_Cred,
                    Cornell_niveles_Post_Assumptions)
  
  Base_Post2_SinTotal <- rbind(Cornell_niveles_Post_Induction,
                     Cornell_niveles_Post_Deduction,
                     Cornell_niveles_Post_Obs_Cred,
                     Cornell_niveles_Post_Assumptions)
  

##Uniendo las dos bases con rbind
  
    #Reducida a 3 column_to_rownames

    Cornell_niveles_Pre_Post<- rbind(Base_Pre,Base_Post)
    
    Cornell_niveles_Pre_Post_SinTotal<- rbind(Base_Pre2_SinTotal,Base_Post2_SinTotal)

    #Base completa con skills en columnas distintas

    Extended_Cornell_niveles_Pre_Post <- rbind(Cornell_niveles_Pre,Cornell_niveles_Post)

4 Gráficos

#Boxplot normal

  Cornell_niveles_Pre_Post %>% 
      ggplot(aes(y=Score,x=Skill,fill= Time))+
      geom_boxplot(alpha = 0.6)+
      scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
      labs(title="Cornell Scores",
           subtitle = "Pre vs Post",
           y="Critical thinking score",
           x="Skill")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),#Tamaños de letra
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))

    Cornell_niveles_Pre_Post %>% 
      ggplot(aes(y=Score,x=Skill,fill= Time))+
      geom_boxplot(alpha = 0.6)+
      scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
      labs(title="Cornell Scores",
           subtitle = "Pre vs Post",
           y="Critical thinking score",
           x="Skill")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),#Tamaños de letra
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))

  #Export
    
  Plot1<- Cornell_niveles_Pre_Post %>% 
  ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

  ggsave(plot = Plot1, filename = "./Graficas_Definitivas/Boxplot_Cornell_scores.png", 
       width = 11, 
       height = 7,
       type = "cairo",
       dpi = "retina")


  ggsave(plot = Plot1, filename = "./Graficas_Definitivas/SVG//Boxplot_Cornell_scores.svg", 
       width = 11, 
       height = 7,
       dpi = "retina")
  

  #SinTotal
  
  Cornell_niveles_Pre_Post_SinTotal %>% 
    ggplot(aes(y=Score,x=Skill,fill= Time))+
    geom_boxplot(alpha = 0.6)+
    scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
    labs(title="Cornell Scores",
         subtitle = "Pre vs Post",
         y="Critical thinking score",
         x="Skill")+
    theme_classic()+
    theme(axis.text.x = element_text( size = 13),#Tamaños de letra
          axis.title.y = element_text( size = 15),
          axis.title.x = element_text( size = 15),
          plot.title = element_text(size = 20,face = "bold"),
          plot.subtitle = element_text(size = 15),
          legend.title = element_text(size = 15),
          legend.text = element_text(size = 13))

  SinTotal1<-Cornell_niveles_Pre_Post_SinTotal %>% 
    ggplot(aes(y=Score,x=Skill,fill= Time))+
    geom_boxplot(alpha = 0.6)+
    scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
    labs(title="Cornell Scores",
         subtitle = "Pre vs Post",
         y="Critical thinking score",
         x="Skill")+
    theme_classic()+
    theme(axis.text.x = element_text( size = 13),#Tamaños de letra
          axis.title.y = element_text( size = 15),
          axis.title.x = element_text( size = 15),
          plot.title = element_text(size = 20,face = "bold"),
          plot.subtitle = element_text(size = 15),
          legend.title = element_text(size = 15),
          legend.text = element_text(size = 13))
  
  #Export
  ggsave(plot = SinTotal1, filename = "./Graficas_Definitivas/SinTotal_Boxplot_Cornell_scores.png", 
         width = 11, 
         height = 7,
         type = "cairo",
         dpi = "retina")

#Boxplot con Geom Point
  
  Cornell_niveles_Pre_Post %>% 
    ggplot(aes(y=Score,x=Skill,fill= Time))+
    geom_boxplot(alpha = 0.6)+
    geom_point(alpha = 0.4)+ #Esta linea agrega los puntos sobre el boxplot
    scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
    labs(title="Cornell Scores",
         subtitle = "Pre vs Post",
         y="Critical thinking score",
         x="Skill")+
    theme_classic()+
    theme(axis.text.x = element_text( size = 13),#Tamaños de letra
          axis.title.y = element_text( size = 15),
          axis.title.x = element_text( size = 15),
          plot.title = element_text(size = 20,face = "bold"),
          plot.subtitle = element_text(size = 15),
          legend.title = element_text(size = 15),
          legend.text = element_text(size = 13))

  #Export
  
  Plot2 <- Cornell_niveles_Pre_Post %>% 
ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  geom_point(alpha = 0.4)+ #Esta linea agrega los puntos sobre el boxplot
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

ggsave(plot = Plot2, filename = "./Graficas_Definitivas/Boxplot_Cornell_scores_GeomPoint.png", 
       width = 11, 
       height = 7,
       type = "cairo",
       dpi = "retina")

ggsave(plot = Plot2, filename = "./Graficas_Definitivas/SVG/Boxplot_Cornell_scores_GeomPoint.svg", 
       width = 11, 
       height = 7,
       dpi = "retina")

#Sintotal

Cornell_niveles_Pre_Post_SinTotal %>% 
  ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  geom_point(alpha = 0.4)+ #Esta linea agrega los puntos sobre el boxplot
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

 Sintotal2<- Cornell_niveles_Pre_Post_SinTotal %>% 
  ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  geom_point(alpha = 0.4)+ #Esta linea agrega los puntos sobre el boxplot
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

  #Export
 ggsave(plot = Sintotal2, filename = "./Graficas_Definitivas/Sintotal2_Boxplot_Cornell_scores_GeomPoint.png", 
        width = 11, 
        height = 7,
        type = "cairo",
        dpi = "retina")



#Boxplot con Jitter

Cornell_niveles_Pre_Post %>% 
  ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  geom_jitter(alpha = 0.6)+
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

  #Export

  Plot3<- Cornell_niveles_Pre_Post %>% 
  ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  geom_jitter(alpha = 0.6)+
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

ggsave(plot = Plot3, filename = "./Graficas_Definitivas/Boxplot_Cornell_scores_Jitter.png", 
       width = 11, 
       height = 7,
       type = "cairo",
       dpi = "retina")

ggsave(plot = Plot3, filename = "./Graficas_Definitivas/SVG//Boxplot_Cornell_scores_Jitter.svg", 
       width = 11, 
       height = 7,
       dpi = "retina")


#Sintotal

Cornell_niveles_Pre_Post_SinTotal %>% 
  ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  geom_jitter(alpha = 0.6)+
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

  Sintotal3 <- Cornell_niveles_Pre_Post_SinTotal %>% 
  ggplot(aes(y=Score,x=Skill,fill= Time))+
  geom_boxplot(alpha = 0.6)+
  geom_jitter(alpha = 0.6)+
  scale_fill_manual(values = c("#F652A0", "#36EEE0"))+
  labs(title="Cornell Scores",
       subtitle = "Pre vs Post",
       y="Critical thinking score",
       x="Skill")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

  
  #Export
  ggsave(plot = Sintotal3, filename = "./Graficas_Definitivas/SinTotal_Boxplot_Cornell_scores_Jitter.png", 
         width = 11, 
         height = 7,
         type = "cairo",
         dpi = "retina")



#Barplot Medias Cornell (Fill con palleta Brewer)

Mean_Cornell_niveles_Pre_Post<-Cornell_niveles_Pre_Post %>% 
  group_by(Skill,Time) %>% 
  summarise(Mean_Cornell=mean(Score))


Mean_Cornell_niveles_Pre_Post %>% 
  ggplot(aes(x=factor(Skill),y=Mean_Cornell, fill = factor(Time))) +
  geom_col(position = "dodge")+
  scale_fill_brewer(palette = "Accent")+
  labs(title="Cornell test scores (Pre vs Post)", 
       subtitle= "By Skill",
       x="Skill",
       y = "Mean score",
       fill="Time")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

#Export

Barplot_Means_Cornell<-Mean_Cornell_niveles_Pre_Post %>% 
  ggplot(aes(x=factor(Skill),y=Mean_Cornell, fill = factor(Time))) +
  geom_col(position = "dodge")+
  scale_fill_brewer(palette = "Accent")+
  labs(title="Cornell test scores (Pre vs Post)", 
       subtitle= "By Skill",
       x="Skill",
       y = "Mean score",
       fill="Time")+
  theme_classic()+
  theme(axis.text.x = element_text( size = 13),#Tamaños de letra
        axis.title.y = element_text( size = 15),
        axis.title.x = element_text( size = 15),
        plot.title = element_text(size = 20,face = "bold"),
        plot.subtitle = element_text(size = 15),
        legend.title = element_text(size = 15),
        legend.text = element_text(size = 13))

  ggsave(plot = Barplot_Means_Cornell, 
       filename = "./Graficas_Definitivas/Barplot_Means_Cornell.png", 
       width = 11, height = 7,
       type= "cairo",
       dpi = "retina")
  
  ggsave(plot = Barplot_Means_Cornell, 
         filename = "./Graficas_Definitivas/SVG/Barplot_Means_Cornell.svg", 
         width = 11, height = 7,
         dpi = "retina")
  
  

# T tests -----------------------------------------------------------------
  
  #Ttests
  
  Cornell_niveles_Pre_Post %>% 
    inference(y =Score , x =Time, statistic = "mean", type = "ht", 
              null = 0, alternative = "greater", method = "theoretical")
## Response variable: numerical
## Explanatory variable: categorical (2 levels) 
## n_Pre = 95, y_bar_Pre = 8.9158, s_Pre = 8.9344
## n_Post = 95, y_bar_Post = 10.9684, s_Post = 9.6618
## H0: mu_Pre =  mu_Post
## HA: mu_Pre > mu_Post
## t = -1.5203, df = 94
## p_value = 0.9341

  Cornell_niveles_Pre_Post %>% 
    inference(y =Score , x =Time, statistic = "mean", type = "ci", 
              null = 0, alternative = "twosided", method = "theoretical")
## Response variable: numerical, Explanatory variable: categorical (2 levels)
## n_Pre = 95, y_bar_Pre = 8.9158, s_Pre = 8.9344
## n_Post = 95, y_bar_Post = 10.9684, s_Post = 9.6618
## 95% CI (Pre - Post): (-4.7334 , 0.6281)

  Ttest_Total_Cornell_niveles_Pre_Post<-Cornell_niveles_Pre_Post %>% 
    filter(Skill=="Total")
  
  #T test corregido
  
  Ttest_Total_Cornell_niveles_Pre_Post %>% 
    inference(y =Score , x =Time, statistic = "mean", type = "ht", 
              null = 0, alternative = "less", method = "theoretical")
## Response variable: numerical
## Explanatory variable: categorical (2 levels) 
## n_Pre = 19, y_bar_Pre = 21.5789, s_Pre = 9.7143
## n_Post = 19, y_bar_Post = 25.9474, s_Post = 9.4838
## H0: mu_Pre =  mu_Post
## HA: mu_Pre < mu_Post
## t = -1.4026, df = 18
## p_value = 0.0889

  #Export
   
  Ttest_total<-t.test(Cornell_niveles_Pre$Total_Cornell, Cornell_niveles_Post$Total_Cornell)
  
  tidy_Ttest_total <- tidy(Ttest_total)  
  
  tidy_Ttest_total %>% 
    dplyr::select(-method) %>% 
    rename(
      "Estimate"=estimate,
      "Mean_Pre"=estimate1,
      "Mean_Post"=estimate2,
      "T-statistic"= statistic,
      "P-value" = p.value,
      "Df"        = parameter,
      "Conf.high"= conf.high,
      "Conf.low" = conf.low
    ) %>%
    kable(
      format   = "html",
      caption  = "T-test for Total Cornell Score (Pre vs Post)",
      booktabs = TRUE,
      digits   = 2
    ) %>%
    kable_styling(
      bootstrap_options = "striped",
      full_width        = FALSE
    ) %>%
    row_spec(row = 0, color = "#000C35") %>% 
    kable_minimal() %>% 
    save_kable("./T_Tests/T_Test_Cornel_Score.png") 
  
  
  
  #Anova
  
  Anova <- aov(Score ~ Skill , data = Cornell_niveles_Pre_Post)

  summary(Anova)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Skill         4  10834  2708.6   88.78 <2e-16 ***
## Residuals   185   5644    30.5                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4.0.1 Correlaciones con Cornell Literacy

#Cornelll Literacy
  
#Renaming
  Cornell_literacy <-  Cornell_literacy %>% 
    rename(Name =`Nom `)
  
sum(is.na(Cornell_literacy))
## [1] 28
#La base tiene 28 Nas. La manera más facil de quitarlos es con  na.omit()
#pero estaría borrando datos innecesariamente si lo hago así

NaOmit_Cornell_literacy <- Cornell_literacy %>% 
  na.omit()


#Comparación tamaño de las bases

length(NaOmit_Cornell_literacy$Pre_MediaLiteracy)
## [1] 29
length(Cornell_literacy$Pre_MediaLiteracy)
## [1] 43
#Lo mejor acá, dado que se trata de Test Distintos es guardarlos
#en bases distintas y a cada una de las bases aplicarles na.omit()


Base_Pre_MediaLiteracy<-Cornell_literacy %>% 
  select(Name,Pre_MediaLiteracy,Pre_Cornell) %>% 
  na.omit()


Base_Pre_IREP<- Cornell_literacy %>% 
  select(Name,Pre_IREP,Pre_Cornell) %>% 
  na.omit()


Base_Pre_Otho3 <- Cornell_literacy %>% 
  select(Name,Pre_Otho3,Pre_Cornell) %>% 
  na.omit()


#Summary Statistics 

  #Base_Pre_MediaLiteracy

    stargazer(as.data.frame(Base_Pre_MediaLiteracy),
          type = "text",
          title = "Summary statistics Pre MediaLiteracy",
          digits = 2,
          style = "jpam", 
          out="./Summary_Tables/Base_Pre_MediaLiteracy.html")
## 
## Summary statistics Pre MediaLiteracy
## -----------------------------------------------------------------------------
## Statistic         N  Mean   SD  Minimum Percentile(25) Percentile(75) Maximum
## -----------------------------------------------------------------------------
## Pre_MediaLiteracy 29 8.66  3.25    3          6              11         15   
## Pre_Cornell       29 24.55 9.54    4          19             30         42   
## -----------------------------------------------------------------------------
  #Base_Pre_IREP
  
    stargazer(as.data.frame(Base_Pre_IREP),
            type = "text",
            title = "Summary statistics Pre IREP",
            digits = 2,
            style = "jpam", 
            out="./Summary_Tables/Base_Pre_IREP.html")
## 
## Summary statistics Pre IREP
## -----------------------------------------------------------------------
## Statistic   N  Mean   SD  Minimum Percentile(25) Percentile(75) Maximum
## -----------------------------------------------------------------------
## Pre_IREP    29 23.79 6.99    8          18             28         38   
## Pre_Cornell 29 24.55 9.54    4          19             30         42   
## -----------------------------------------------------------------------
  #Base_Pre_Otho3
  
    
    stargazer(as.data.frame(Base_Pre_Otho3),
              type = "text",
              title = "Summary statistics Base Pre Otho3",
              digits = 2,
              style = "jpam", 
              out="./Summary_Tables/Base_Pre_Otho3.html")
## 
## Summary statistics Base Pre Otho3
## -----------------------------------------------------------------------
## Statistic   N  Mean   SD  Minimum Percentile(25) Percentile(75) Maximum
## -----------------------------------------------------------------------
## Pre_Otho3   29 25.48 6.00    9          23             30         34   
## Pre_Cornell 29 24.55 9.54    4          19             30         42   
## -----------------------------------------------------------------------
#Scatterplots
    
  #Base_Pre_IREP
    
    Base_Pre_IREP %>% 
      ggplot(aes(y=Pre_Cornell,x=Pre_IREP))+
      geom_point()+
      geom_smooth(method=lm, color="darkred", fill="#36EEE0")+
      labs(title="Cornell vs IREP")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))

    #Export
    Scatter_Base_Pre_IREP<- Base_Pre_IREP %>% 
      ggplot(aes(y=Pre_Cornell,x=Pre_IREP))+
      geom_point()+
      geom_smooth(method=lm, color="darkred", fill="#36EEE0")+
      labs(title="Cornell vs IREP")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))
    
    
    ggsave(plot = Scatter_Base_Pre_IREP, 
           filename = "./Graficas_Definitivas/Scatter_Base_Pre_IREP.png", 
           width = 9, height = 7,
           type= "cairo",
           dpi = "retina")
    
    ggsave(plot = Scatter_Base_Pre_IREP, 
           filename = "./Graficas_Definitivas/SVG/Scatter_Base_Pre_IREP.svg", 
           width = 9, height = 7,
           dpi = "retina")

    
    
  #Base_Pre_MediaLiteracy
    
    Base_Pre_MediaLiteracy %>% 
      ggplot(aes(y=Pre_Cornell,x=Pre_MediaLiteracy))+
      geom_point()+
      geom_smooth(method=lm, color="darkred", fill="#36EEE0")+
      labs(title="Cornell vs MediaLiteracy")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))

  #Export
    
    Scatter_Base_Pre_MediaLiteracy<- Base_Pre_MediaLiteracy %>% 
      ggplot(aes(y=Pre_Cornell,x=Pre_MediaLiteracy))+
      geom_point()+
      geom_smooth(method=lm, color="darkred", fill="#36EEE0")+
      labs(title="Cornell vs MediaLiteracy")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))
    
    
    ggsave(plot = Scatter_Base_Pre_MediaLiteracy, 
           filename = "./Graficas_Definitivas/Scatter_Base_Pre_MediaLiteracy.png", 
           width = 9, height = 7,
           type= "cairo",
           dpi = "retina")
    
    ggsave(plot = Scatter_Base_Pre_MediaLiteracy, 
           filename = "./Graficas_Definitivas/SVG/Scatter_Base_Pre_MediaLiteracy.svg", 
           width = 9, height = 7,
           dpi = "retina")
    
    
  
  #Base_Pre_Otho3
    
    Base_Pre_Otho3 %>% 
      ggplot(aes(y=Pre_Cornell,x= Pre_Otho3))+
      geom_point()+
      geom_smooth(method=lm, color="darkred", fill="#36EEE0")+
      labs(title="Cornell vs Otho3")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))

    #Export
    
    Scatter_Base_Pre_Otho3 <- Base_Pre_Otho3 %>% 
      ggplot(aes(y=Pre_Cornell,x= Pre_Otho3))+
      geom_point()+
      geom_smooth(method=lm, color="darkred", fill="#36EEE0")+
      labs(title="Cornell vs Otho3")+
      theme_classic()+
      theme(axis.text.x = element_text( size = 13),
            axis.title.y = element_text( size = 15),
            axis.title.x = element_text( size = 15),
            plot.title = element_text(size = 20,face = "bold"),
            plot.subtitle = element_text(size = 15),
            legend.title = element_text(size = 15),
            legend.text = element_text(size = 13))
    
    
    ggsave(plot = Scatter_Base_Pre_Otho3, 
           filename = "./Graficas_Definitivas/Scatter_Base_Pre_Otho3.png", 
           width = 9, height = 7,
           type= "cairo",
           dpi = "retina")
    
    ggsave(plot = Scatter_Base_Pre_Otho3, 
           filename = "./Graficas_Definitivas/SVG/Scatter_Base_Pre_Otho3.svg", 
           width = 9, height = 7,
           dpi = "retina")
    
    
  #Los tres Scatter en un solo gráfico
    
    ggarrange(Scatter_Base_Pre_IREP,
              Scatter_Base_Pre_MediaLiteracy,
              Scatter_Base_Pre_Otho3,
              nrow = 1,ncol = 3)

    #Export
    
    SBS_Scatter_TestsVsCornell <- ggarrange(Scatter_Base_Pre_IREP,
                Scatter_Base_Pre_MediaLiteracy,
                Scatter_Base_Pre_Otho3,
                nrow = 1,ncol = 3)

    ggsave(plot = SBS_Scatter_TestsVsCornell, 
           filename = "./Graficas_Definitivas/SBS_Scatter_TestsVsCornell.png", 
           width = 16, height = 7,
           type= "cairo",
           dpi = "retina")
    
    
    ggsave(plot = SBS_Scatter_TestsVsCornell, 
           filename = "./Graficas_Definitivas/SVG/SBS_Scatter_TestsVsCornell.svg", 
           width = 16, height = 7,
           dpi = "retina")

5 Corplots

  #Usando Corplot
    
    ForCorplot_Cornell_literacy<-Cornell_literacy %>% 
      select(-Name)
    
    corrplot.mixed(cor(ForCorplot_Cornell_literacy,use = "pairwise.complete.obs"),
                   lower = "ellipse", 
                   upper = "number",
                   tl.col = "black",
                   title="Correlaciones Cornell Literacy")

    #Export
    
    png(height=1800, width=1800, file="./Correlation_Plots/Cornell_literacy.png", type = "cairo",res = 300)
    
    
    corrplot.mixed(cor(ForCorplot_Cornell_literacy,use = "pairwise.complete.obs"),
                   lower = "ellipse", 
                   upper = "number",
                   tl.col = "black",
                   title="Correlaciones Cornell Literacy")
    
    dev.off()
## png 
##   2
    #Usando ggcorrplot
    
    ggcorrplot(cor(ForCorplot_Cornell_literacy,use = "pairwise.complete.obs"),
               hc.order=TRUE, type='lower',lab = TRUE,
               title = "Correlaciones Well Being",
               ggtheme = ggplot2::theme_minimal(),
               colors = c("#F652A0","white","#36EEE0"))

    #Export
    
    ggcorrplot_ForCorplot_Cornell_literacy  <- ggcorrplot(cor(ForCorplot_Cornell_literacy,use = "pairwise.complete.obs"),
                          hc.order=TRUE, type='lower',lab = TRUE,
                          title = "Correlaciones Well Being",
                          ggtheme = ggplot2::theme_minimal(),
                          colors = c("#F652A0","white","#36EEE0"))
    
    ggsave(plot = ggcorrplot_ForCorplot_Cornell_literacy, filename = "./Correlation_Plots/ggcorrplot_ForCorplot_Cornell_literacy.png", 
           width = 9, 
           height = 7,
           type = "cairo",
           dpi = "retina")

6 Tablas de Regresión

 #Cuando hago las tablas con Stargazer pongo "Text" en type
    #Para que la tabla me aparezca en el programa, pero dejo
    #el formato de out en ".html"
    
    
  #Base_Pre_IREP
    
    m1_Base_Pre_IREP<-lm( Pre_Cornell ~ Pre_IREP,
                             data = Base_Pre_IREP)

     #Export
    
     stargazer(m1_Base_Pre_IREP,
               ci = TRUE,
               type = "text",
               title="Pre Cornell ~ Pre IREP",
               align=TRUE,
               out = "Regression_Tables/m1_Base_Pre_IREP.html")
## 
## Pre Cornell ~ Pre IREP
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                             Pre_Cornell        
## -----------------------------------------------
## Pre_IREP                     0.669***          
##                           (0.221, 1.118)       
##                                                
## Constant                       8.624           
##                          (-2.485, 19.732)      
##                                                
## -----------------------------------------------
## Observations                    29             
## R2                             0.241           
## Adjusted R2                    0.213           
## Residual Std. Error       8.467 (df = 27)      
## F Statistic            8.556*** (df = 1; 27)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
  #Base_Pre_MediaLiteracy
     
     m1_Base_Pre_MediaLiteracy<-lm( Pre_Cornell ~ Pre_MediaLiteracy,
                           data = Base_Pre_MediaLiteracy)
     
     
     #Export
     
     stargazer(m1_Base_Pre_MediaLiteracy,
               ci = TRUE,
               type = "text",
               title="Pre Cornell ~ Pre MediaLiteracy",
               align=TRUE,
               out = "Regression_Tables/m1_Base_Pre_MediaLiteracy.html")
## 
## Pre Cornell ~ Pre MediaLiteracy
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                             Pre_Cornell        
## -----------------------------------------------
## Pre_MediaLiteracy              0.376           
##                           (-0.723, 1.476)      
##                                                
## Constant                     21.295***         
##                          (11.152, 31.438)      
##                                                
## -----------------------------------------------
## Observations                    29             
## R2                             0.016           
## Adjusted R2                   -0.020           
## Residual Std. Error       9.637 (df = 27)      
## F Statistic             0.450 (df = 1; 27)     
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
  #Base_Pre_Otho3
     
     m1_Base_Pre_Otho3<-lm( Pre_Cornell ~ Pre_Otho3,
                                    data = Base_Pre_Otho3)

     #Export
     
     stargazer(m1_Base_Pre_Otho3,
               ci = TRUE,
               type = "text",
               title="Pre Cornell ~ Pre Otho3",
               align=TRUE,
               out = "Regression_Tables/m1_Base_Pre_Otho3.html")
## 
## Pre Cornell ~ Pre Otho3
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                             Pre_Cornell        
## -----------------------------------------------
## Pre_Otho3                      0.449           
##                           (-0.126, 1.024)      
##                                                
## Constant                      13.107*          
##                          (-1.934, 28.148)      
##                                                
## -----------------------------------------------
## Observations                    29             
## R2                             0.080           
## Adjusted R2                    0.046           
## Residual Std. Error       9.321 (df = 27)      
## F Statistic             2.343 (df = 1; 27)     
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
  #Los tres modelos en una misma tabla
     
     #Export
     stargazer( m1_Base_Pre_IREP,
                m1_Base_Pre_MediaLiteracy,
                m1_Base_Pre_Otho3,
               ci = TRUE,
               type = "text",
               title="Pre Cornell ~ Literacy Tests",
               align=TRUE,
               out = "Regression_Tables/m1_Full_Table.html")
## 
## Pre Cornell ~ Literacy Tests
## ================================================================================
##                                              Dependent variable:                
##                               --------------------------------------------------
##                                                  Pre_Cornell                    
##                                     (1)              (2)              (3)       
## --------------------------------------------------------------------------------
## Pre_IREP                          0.669***                                      
##                                (0.221, 1.118)                                   
##                                                                                 
## Pre_MediaLiteracy                                   0.376                       
##                                                (-0.723, 1.476)                  
##                                                                                 
## Pre_Otho3                                                            0.449      
##                                                                 (-0.126, 1.024) 
##                                                                                 
## Constant                           8.624          21.295***         13.107*     
##                               (-2.485, 19.732) (11.152, 31.438) (-1.934, 28.148)
##                                                                                 
## --------------------------------------------------------------------------------
## Observations                         29               29               29       
## R2                                 0.241            0.016            0.080      
## Adjusted R2                        0.213            -0.020           0.046      
## Residual Std. Error (df = 27)      8.467            9.637            9.321      
## F Statistic (df = 1; 27)          8.556***          0.450            2.343      
## ================================================================================
## Note:                                                *p<0.1; **p<0.05; ***p<0.01