1 Paquetes

#install.packages("easypackages")
library(easypackages)

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()
#Hay que instalar webshot::install_phantomjs() para que kable se pueda exportar como png 
#(Solo instalar. No hace falta usar library luego)
#library(plotly) Oculta Select

1.1 Otras formas de cargar paquetes

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


#install.packages(c("boot","ggm","ggplot2","polycor","Hmisc","dplyr",
"readxl","devtools","tidyverse","lubridate",
"ggridges","wesanderson","RColorBrewer","knitr",
"kableExtra","hrbrthemes","statsr","stargazer","psych",
"corrplot","corrr","GGally","ggcorrplot","PerformanceAnalytics",
"pander","broom","purr","kableExtra","egg"))

2 Workspace

#Directorio


dir.create("Correlation_Matrices_html")
dir.create("Summary_Tables")
dir.create("Correlation_Plots")
dir.create("Regression_Tables")
dir.create("Graficas_Definitivas")



#Cargando datos

data_final_well_being <- read_excel("data_final_well-being.xlsx")

3 Exploración de datos

#Exploración inicial

head(data_final_well_being) #Primeras 10 filas
## # A tibble: 6 x 8
##      ID green   SHS anxdep space   SES  parc sport
##   <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     1  0.13  6.75   2      0.5    89     3     6
## 2     2  0.28  6.25   1.02   1      73     1     3
## 3     3  0.13  3.5    1.41   1      89     2     4
## 4     4  0.25  7      1      1     113     3     6
## 5     5  0.25  7      1.39   1     113     3     6
## 6     6  0.19  4      2.14   1.4    81     3     3
tail(data_final_well_being) #Últimas 6 filas
## # A tibble: 6 x 8
##      ID green   SHS anxdep space   SES  parc sport
##   <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
## 1   310  0.28  4.5    1.21     1    73     2     4
## 2   311  0.13  3.5    1.39     2    89     2     2
## 3   312  0.21  4      1.39     1    95     3     6
## 4   313  0.28  4      1.14     2    69     3     4
## 5   314  0.12  6.25   2.22     2    66     2     5
## 6   315  0.29  4      1.14     1    81     1     1
dim(data_final_well_being) #Número de filas y número de columnas
## [1] 315   8
str(data_final_well_being) #Estructura de la bas (Tipos de variables)
## tibble [315 x 8] (S3: tbl_df/tbl/data.frame)
##  $ ID    : num [1:315] 1 2 3 4 5 6 7 8 9 10 ...
##  $ green : num [1:315] 0.13 0.28 0.13 0.25 0.25 0.19 0.19 0.19 0.06 0.19 ...
##  $ SHS   : num [1:315] 6.75 6.25 3.5 7 7 4 7 6.75 5.25 4 ...
##  $ anxdep: num [1:315] 2 1.02 1.41 1 1.39 ...
##  $ space : num [1:315] 0.5 1 1 1 1 ...
##  $ SES   : num [1:315] 89 73 89 113 113 81 81 81 80 81 ...
##  $ parc  : num [1:315] 3 1 2 3 3 3 3 1 2 2 ...
##  $ sport : num [1:315] 6 3 4 6 6 3 5 1 6 2 ...
glimpse(data_final_well_being) #Preview de los datos don Dplyr
## Rows: 315
## Columns: 8
## $ ID     <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, ~
## $ green  <dbl> 0.13, 0.28, 0.13, 0.25, 0.25, 0.19, 0.19, 0.19, 0.06, 0.19, 0.1~
## $ SHS    <dbl> 6.75, 6.25, 3.50, 7.00, 7.00, 4.00, 7.00, 6.75, 5.25, 4.00, 5.5~
## $ anxdep <dbl> 2.000000, 1.017647, 1.405882, 1.000000, 1.394118, 2.137255, 1.1~
## $ space  <dbl> 0.5000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.400000~
## $ SES    <dbl> 89, 73, 89, 113, 113, 81, 81, 81, 80, 81, 89, 112, 66, 81, 89, ~
## $ parc   <dbl> 3, 1, 2, 3, 3, 3, 3, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 2, 2, 3, 1, ~
## $ sport  <dbl> 6, 3, 4, 6, 6, 3, 5, 1, 6, 2, 5, 2, 4, 3, 6, 3, 5, 6, 1, 1, 1, ~
summary(data_final_well_being) # Estadísticos descriptivos
##        ID            green           SHS            anxdep          space      
##  Min.   :  1.0   Min.   :0.04   Min.   :1.750   Min.   :1.000   Min.   :0.000  
##  1st Qu.: 79.5   1st Qu.:0.13   1st Qu.:3.500   1st Qu.:1.343   1st Qu.:0.750  
##  Median :158.0   Median :0.25   Median :4.250   Median :1.743   Median :1.000  
##  Mean   :158.0   Mean   :0.26   Mean   :4.524   Mean   :1.843   Mean   :1.166  
##  3rd Qu.:236.5   3rd Qu.:0.33   3rd Qu.:5.500   3rd Qu.:2.227   3rd Qu.:1.500  
##  Max.   :315.0   Max.   :0.71   Max.   :7.000   Max.   :3.914   Max.   :4.000  
##                                                                 NA's   :3      
##       SES             parc           sport      
##  Min.   : 51.0   Min.   :1.000   Min.   :1.000  
##  1st Qu.: 69.0   1st Qu.:1.000   1st Qu.:1.000  
##  Median : 81.0   Median :2.000   Median :3.000  
##  Mean   : 84.9   Mean   :1.956   Mean   :3.479  
##  3rd Qu.:103.0   3rd Qu.:3.000   3rd Qu.:5.000  
##  Max.   :113.0   Max.   :3.000   Max.   :6.000  
## 

3.0.1 ¿Hay NAS?

sum(is.na(data_final_well_being))
## [1] 3
sum(is.na(data_final_well_being$space))
## [1] 3
length(data_final_well_being$space)
## [1] 315
A<-data_final_well_being %>% 
  na.omit()

sum(is.na(A))
## [1] 0
length(A$space)
## [1] 312

3.0.2 Formato de las columnas

“Parc” es una variable con pocos niveles. Asumamos que la quiero tratar, no como una variable numérica, sino como una categórica. Es decir, como un factor. Voy a crear una segunda base para hacer la demostración.

Prueba <-data_final_well_being

unique(data_final_well_being$parc)
## [1] 3 1 2
Prueba$parc <- as.factor(Prueba$green)
Prueba$sport <- as.integer(Prueba$sport)

str(Prueba)
## tibble [315 x 8] (S3: tbl_df/tbl/data.frame)
##  $ ID    : num [1:315] 1 2 3 4 5 6 7 8 9 10 ...
##  $ green : num [1:315] 0.13 0.28 0.13 0.25 0.25 0.19 0.19 0.19 0.06 0.19 ...
##  $ SHS   : num [1:315] 6.75 6.25 3.5 7 7 4 7 6.75 5.25 4 ...
##  $ anxdep: num [1:315] 2 1.02 1.41 1 1.39 ...
##  $ space : num [1:315] 0.5 1 1 1 1 ...
##  $ SES   : num [1:315] 89 73 89 113 113 81 81 81 80 81 ...
##  $ parc  : Factor w/ 16 levels "0.04","0.06",..: 5 11 5 10 10 7 7 7 2 7 ...
##  $ sport : int [1:315] 6 3 4 6 6 3 5 1 6 2 ...

3.0.3 Cambiando el formato de varias variables al tiempo

Prueba[,c(2,3,5, 7)] <- lapply(Prueba[,c(2, 3, 5, 7)], as.factor)
str(Prueba)
## tibble [315 x 8] (S3: tbl_df/tbl/data.frame)
##  $ ID    : num [1:315] 1 2 3 4 5 6 7 8 9 10 ...
##  $ green : Factor w/ 16 levels "0.04","0.06",..: 5 11 5 10 10 7 7 7 2 7 ...
##  $ SHS   : Factor w/ 23 levels "1.75","2","2.25",..: 22 20 8 23 23 11 23 22 16 11 ...
##  $ anxdep: num [1:315] 2 1.02 1.41 1 1.39 ...
##  $ space : Factor w/ 35 levels "0","0.0666666666666667",..: 10 18 18 18 18 23 18 22 18 18 ...
##  $ SES   : num [1:315] 89 73 89 113 113 81 81 81 80 81 ...
##  $ parc  : Factor w/ 16 levels "0.04","0.06",..: 5 11 5 10 10 7 7 7 2 7 ...
##  $ sport : int [1:315] 6 3 4 6 6 3 5 1 6 2 ...
Prueba[,c(1:8)] <- lapply(Prueba[,c(1:8)], as.numeric)
str(Prueba)
## tibble [315 x 8] (S3: tbl_df/tbl/data.frame)
##  $ ID    : num [1:315] 1 2 3 4 5 6 7 8 9 10 ...
##  $ green : num [1:315] 5 11 5 10 10 7 7 7 2 7 ...
##  $ SHS   : num [1:315] 22 20 8 23 23 11 23 22 16 11 ...
##  $ anxdep: num [1:315] 2 1.02 1.41 1 1.39 ...
##  $ space : num [1:315] 10 18 18 18 18 23 18 22 18 18 ...
##  $ SES   : num [1:315] 89 73 89 113 113 81 81 81 80 81 ...
##  $ parc  : num [1:315] 5 11 5 10 10 7 7 7 2 7 ...
##  $ sport : num [1:315] 6 3 4 6 6 3 5 1 6 2 ...

4 Correlaciones y Stargazer

4.0.1 Correlación con NAS:

La columna space tiene NAS, entonces tengo que introducir el argumento “pairwise.complete.obs” en la función “Cor” para que no los considere al momento de computar la correlación.

#COR (Con Nas)

cor(data_final_well_being)
##                 ID       green         SHS      anxdep space         SES
## ID      1.00000000 -0.01410288 -0.15002031  0.03141717    NA -0.11862430
## green  -0.01410288  1.00000000  0.01372699 -0.05102805    NA  0.49430894
## SHS    -0.15002031  0.01372699  1.00000000 -0.35981163    NA  0.09566740
## anxdep  0.03141717 -0.05102805 -0.35981163  1.00000000    NA -0.09909533
## space           NA          NA          NA          NA     1          NA
## SES    -0.11862430  0.49430894  0.09566740 -0.09909533    NA  1.00000000
## parc   -0.09858559  0.01341554  0.15548518 -0.17129297    NA  0.08624957
## sport  -0.09035455  0.01467706  0.22174085 -0.14396809    NA  0.10921363
##               parc       sport
## ID     -0.09858559 -0.09035455
## green   0.01341554  0.01467706
## SHS     0.15548518  0.22174085
## anxdep -0.17129297 -0.14396809
## space           NA          NA
## SES     0.08624957  0.10921363
## parc    1.00000000  0.62688910
## sport   0.62688910  1.00000000
cor(data_final_well_being,use = "pairwise.complete.obs")
##                  ID       green         SHS      anxdep        space
## ID      1.000000000 -0.01410288 -0.15002031  0.03141717  0.008712926
## green  -0.014102877  1.00000000  0.01372699 -0.05102805  0.094127726
## SHS    -0.150020311  0.01372699  1.00000000 -0.35981163  0.059252039
## anxdep  0.031417167 -0.05102805 -0.35981163  1.00000000 -0.079107367
## space   0.008712926  0.09412773  0.05925204 -0.07910737  1.000000000
## SES    -0.118624296  0.49430894  0.09566740 -0.09909533  0.078032688
## parc   -0.098585585  0.01341554  0.15548518 -0.17129297  0.146085372
## sport  -0.090354550  0.01467706  0.22174085 -0.14396809  0.116189962
##                SES        parc       sport
## ID     -0.11862430 -0.09858559 -0.09035455
## green   0.49430894  0.01341554  0.01467706
## SHS     0.09566740  0.15548518  0.22174085
## anxdep -0.09909533 -0.17129297 -0.14396809
## space   0.07803269  0.14608537  0.11618996
## SES     1.00000000  0.08624957  0.10921363
## parc    0.08624957  1.00000000  0.62688910
## sport   0.10921363  0.62688910  1.00000000

4.0.2 Cor sin la columna de ID

data_final_well_being %>% 
  select(2:8) %>% 
  cor(use = "pairwise.complete.obs")
##              green         SHS      anxdep       space         SES        parc
## green   1.00000000  0.01372699 -0.05102805  0.09412773  0.49430894  0.01341554
## SHS     0.01372699  1.00000000 -0.35981163  0.05925204  0.09566740  0.15548518
## anxdep -0.05102805 -0.35981163  1.00000000 -0.07910737 -0.09909533 -0.17129297
## space   0.09412773  0.05925204 -0.07910737  1.00000000  0.07803269  0.14608537
## SES     0.49430894  0.09566740 -0.09909533  0.07803269  1.00000000  0.08624957
## parc    0.01341554  0.15548518 -0.17129297  0.14608537  0.08624957  1.00000000
## sport   0.01467706  0.22174085 -0.14396809  0.11618996  0.10921363  0.62688910
##              sport
## green   0.01467706
## SHS     0.22174085
## anxdep -0.14396809
## space   0.11618996
## SES     0.10921363
## parc    0.62688910
## sport   1.00000000
Correlation_Matrix <-data_final_well_being %>% 
  select(2:8) %>% 
  cor(use = "pairwise.complete.obs")

4.0.3 Correlation Matrix con Stargazer

#Para visualizar en R y en Markdown

stargazer(Correlation_Matrix,
          title="Correlation Matrix",
          type = "text")
## 
## Correlation Matrix
## =======================================================
##        green   SHS   anxdep space   SES    parc  sport 
## -------------------------------------------------------
## green    1    0.014  -0.051 0.094  0.494  0.013  0.015 
## SHS    0.014    1    -0.360 0.059  0.096  0.155  0.222 
## anxdep -0.051 -0.360   1    -0.079 -0.099 -0.171 -0.144
## space  0.094  0.059  -0.079   1    0.078  0.146  0.116 
## SES    0.494  0.096  -0.099 0.078    1    0.086  0.109 
## parc   0.013  0.155  -0.171 0.146  0.086    1    0.627 
## sport  0.015  0.222  -0.144 0.116  0.109  0.627    1   
## -------------------------------------------------------
#Para Exportar como objeto html para meter en word
  
  stargazer(Correlation_Matrix,
          title="Correlation Matrix",
          type = "html",
          out = "./Correlation_Matrices_html/Correlation_Matrix.html")

4.0.4 Summary Statistics con Stargazer:

Stargazer solo funciona con Data Frames. Por ende es necesario utilizar as.data.frame. Con Tibbles No funciona.

  stargazer(as.data.frame(data_final_well_being),
            type = "text",
            title = "Well Being Summary")
## 
## Well Being Summary
## ============================================================
## Statistic  N   Mean   St. Dev.  Min  Pctl(25) Pctl(75)  Max 
## ------------------------------------------------------------
## ID        315 158.000  91.077    1     79.5    236.5    315 
## green     315  0.260   0.152   0.040  0.130    0.330   0.710
## SHS       315  4.524   1.326   1.750  3.500    5.500   7.000
## anxdep    315  1.843   0.613     1     1.3      2.2      4  
## space     312  1.166   0.687   0.000  0.750    1.500   4.000
## SES       315 84.902   17.995   51      69      103     113 
## parc      315  1.956   0.828     1      1        3       3  
## sport     315  3.479   1.857     1      1        5       6  
## ------------------------------------------------------------
  #Exportar
  stargazer(as.data.frame(data_final_well_being),
            type = "html",
            title = "Well Being Summary",
            out="./Summary_Tables/Well_Being_Summary.html")

5 Correlation plots:

Para las gráficas de correlación puede ser útil crear antes una nueva base sin la columna “Id”

#Nueva Base sin columna de ID
  
data_final_well_being_NoId <-data_final_well_being %>% 
  select(c(2:8)) 
  
head(data_final_well_being_NoId)
## # A tibble: 6 x 7
##   green   SHS anxdep space   SES  parc sport
##   <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
## 1  0.13  6.75   2      0.5    89     3     6
## 2  0.28  6.25   1.02   1      73     1     3
## 3  0.13  3.5    1.41   1      89     2     4
## 4  0.25  7      1      1     113     3     6
## 5  0.25  7      1.39   1     113     3     6
## 6  0.19  4      2.14   1.4    81     3     3
plot(data_final_well_being_NoId)

5.0.1 Corplots con diferentes paquetes

opts_chunk$set(fig.width=18, fig.height=10)

###Corplot1
plot(data_final_well_being_NoId)

#Export

    png("./Correlation_Plots/Corplot_1.png",
     width = 2000, height = 1800,type = "cairo",res=300)

    plot(data_final_well_being_NoId)

    dev.off()
## png 
##   2
###Corplot2    
    
  #Omitir ID primera columna sin crear una nueva base

  data_final_well_being %>% 
  select(c(2:8)) %>% 
  corPlot(cex = 1.2)

    #Export
  
      png("./Correlation_Plots/Corplot_2.png",
           width = 2000, height = 1800,type="cairo",res=250)
      
      data_final_well_being %>% 
        select(c(2:8)) %>% 
        corPlot(cex = 1.2)
      
      dev.off()
## png 
##   2
#Con El paquete Corplot 
      
###Corplot 2A
    corrplot(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),   
               method = "square", # Correlation plot method
               type = "full",    # Correlation plot style (also "upper" and "lower")
               diag = TRUE,      # If TRUE (default), adds the diagonal
               tl.col = "black", # Labels color
               bg = "white",     # Background color
               title = "Correlaciones Well Being", # Main title
               col = NULL)

  #Export
  png(height=1800, width=1800, file="./Correlation_Plots/Corplot_2A.png", 
      type = "cairo",res = 300)
      
  corrplot(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),   
               method = "square", # Correlation plot method
               type = "full",    # Correlation plot style (also "upper" and "lower")
               diag = TRUE,      # If TRUE (default), adds the diagonal
               tl.col = "black", # Labels color
               bg = "white",     # Background color
               title = "Correlaciones Well Being", # Main title
               col = NULL)

  dev.off()
## png 
##   2
###Corplot 3
  corrplot(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),        
         method = "shade", 
         type = "lower",    
         diag = TRUE,      
         tl.col = "black", 
         bg = "white",     
         title = "Correlaciones Well Being", # Main title
         col = NULL)

  #Export
  png(height=1800, width=1800, file="./Correlation_Plots/Corplot_3.png", type = "cairo",res = 300)

  corrplot(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),        
         method = "shade", 
         type = "lower",    
         diag = TRUE,      
         tl.col = "black", 
         bg = "white",     
         title = "Correlaciones Well Being", # Main title
         col = NULL)

  dev.off()
## png 
##   2
###Corplot 4
  corrplot.mixed(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
                 lower = "circle", 
                 upper = "number",
                 tl.col = "black",
                 title="Correlaciones Well_Being")

  #Export
  png(height=1800, width=1800, file="./Correlation_Plots/Corplot_4.png", type = "cairo",res = 300)

  corrplot.mixed(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
               lower = "circle", 
               upper = "number",
               tl.col = "black",
               title="Correlaciones Well_Being")

  dev.off()
## png 
##   2
###Corplot 5
  corrplot.mixed(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
                 lower = "ellipse", 
                 upper = "number",
                 tl.col = "black",
                 title="Correlaciones Well_Being")

  #Export
  png(height=1800, width=1800, file="./Correlation_Plots/Corplot_5.png", type = "cairo",res = 300)


  corrplot.mixed(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
               lower = "ellipse", 
               upper = "number",
               tl.col = "black",
               title="Correlaciones Well_Being")

dev.off()
## png 
##   2
###Corplot 6

  corrplot.mixed(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
               lower = "pie", 
               upper = "number",
               tl.col = "black",
               title="Correlaciones Well_Being")

  #Export
  png(height=1800, width=1800, file="./Correlation_Plots/Corplot_6.png", type = "cairo",res = 300)

  corrplot.mixed(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
               lower = "pie", 
               upper = "number",
               tl.col = "black",
               title="Correlaciones Well_Being")
  dev.off()
## png 
##   2
###Corplot 7

  chart.Correlation(data_final_well_being_NoId, histogram=TRUE, pch=19,
                    method = "pearson")

  #Export
  png("./Correlation_Plots/Corplot_7.png",
      width = 2000, height = 1800,type = "cairo",res=300)

  chart.Correlation(data_final_well_being_NoId, histogram=TRUE, pch=19,
                  method = "pearson")
  dev.off()
## png 
##   2
###Corplot 8 (ggplot)
  
ggpairs(data_final_well_being_NoId,
          title = "Correlaciones Well being")

  #Export
  Plot8<-ggpairs(data_final_well_being_NoId,
        title = "Correlaciones Well being")

  ggsave(plot = Plot8, filename = "./Correlation_Plots/Corplot_8.png", 
       width = 11, 
       height = 7,
       type = "cairo",
       dpi = "retina")

###Corplot 9 (ggplot)

ggcorrplot(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
             hc.order=TRUE, type='lower',lab = TRUE,
             title = "Correlaciones Well Being")

  #Export

  Plot9<-ggcorrplot(cor(data_final_well_being_NoId,use = "pairwise.complete.obs"),
           hc.order=TRUE, type='lower',lab = TRUE,
           title = "Correlaciones Well Being")

  ggsave(plot = Plot9, filename = "./Correlation_Plots/Corplot_9.png", 
       width = 9, 
       height = 7,
       type = "cairo",
       dpi = "retina")

6 Gráficas Básicas con Ggplot

#Histograma básico: Green
  
data_final_well_being %>% 
  ggplot(aes(x=green))+
  geom_histogram(fill="gray", color="black", bins=30)+
  ggtitle("Histograma de espacio verde disponible")+
  labs(x="Edad (años)", y="Frecuencia")

#Histopgrama básico: SHS
data_final_well_being %>% 
  ggplot(aes(x=SHS))+
  geom_histogram(fill="royalblue4")+
  ggtitle("Histograma de SHS")+
  labs(x="SHS", y="Frecuencia")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Histograma anxdep con distribución normal

data_final_well_being %>% ggplot(aes(x=anxdep, y=..density..))+
  geom_histogram(fill="royalblue4", color="green")+
  geom_line(aes(x=anxdep, y=dnorm(anxdep, mean(anxdep), sd(anxdep))), color="red", size=1)+
  ggtitle("Histograma de edad con curva normal")+
  labs(x="Edad (años)", y="Densidad")+
  theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Espacio Verde

data_final_well_being %>% 
  ggplot(aes(x=green))+
  geom_histogram(fill="seagreen4")+
  ggtitle("Histograma de espacio verde disponible")+
  labs(x="Espacio Verde", y="Frecuencia")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

###Boxplots

  #Básico (Usando nomenclatura Dplyr)
  data_final_well_being %>%
    ggplot(aes(y=anxdep)) +
  geom_boxplot()

  #Básico (sin Dplyr)
  ggplot(data=data_final_well_being,aes(y=anxdep))+
    geom_boxplot()

  #Completo. Usando Labels, y filtrando por Sport. Dejando eje X en blanco
  
  data_final_well_being %>%
  filter(sport==6) %>% 
  na.omit() %>%  #No es necesario acá pero es bueno saber que se puede usar
  ggplot(aes(y=anxdep))+
  geom_boxplot(fill="springgreen4")+
  labs(title = "Nivel de ansiedad y depresión",
       subtitle = "Frecuencia deporte = 6",
       y="Nivel de ansiedad y depresión")+
  scale_fill_brewer(palette = "Set1")+
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

#Side by Side Boxplots (Anxdep_según Sport) Completo. Usando Labels y Brewer Palette
  
data_final_well_being %>%
    ggplot(aes(y=anxdep,x=factor(sport)))+
    geom_boxplot(aes(fill=as.factor(sport)))+
    labs(title = "Nivel de ansiedad y depresión",
         subtitle = "Según Frecuencia deporte",
         y="Nivel de ansiedad y depresión",
         x= "Frecuencia con la cual la persona practica deporte",
         fill="Frec deporte")+
    scale_fill_brewer(palette = "RdPu")+
  theme_classic()

  #Export
 
  SBS_Boxplot_Anxdep_Sport<-data_final_well_being %>%
   ggplot(aes(y=anxdep,x=factor(sport)))+
   geom_boxplot(aes(fill=as.factor(sport)))+
   labs(title = "Nivel de ansiedad y depresión",
        subtitle = "Según Frecuencia deporte",
        y="Nivel de ansiedad y depresión",
        x= "Frecuencia con la cual la persona practica deporte",
        fill="Frec deporte")+
   scale_fill_brewer(palette = "RdPu")+
    theme_classic()
 
  ggsave(plot = SBS_Boxplot_Anxdep_Sport, 
       filename = "./Graficas_Definitivas/SBS_Boxplot_Anxdep_Sport.png", 
       width = 11, height = 7,
       type= "cairo",
       dpi = "retina")


  
#Side by Sied Boxplots (Anxdep según Parc) 
  
  data_final_well_being %>%
    ggplot(aes(y=anxdep,x=factor(parc)))+
    geom_boxplot(aes(fill=as.factor(parc)))+
    labs(title = "Nivel de ansiedad y depresión",
         subtitle = "Según Frecuencia parque",
         y="Nivel de ansiedad y depresión",
         x= "Frecuencia con la cual la persona visita el parque",
         fill="Frec deporte")+
    scale_fill_brewer(palette = "RdPu")+
    theme_classic()

  #Export
  
  SBS_Boxplot_Anxdep_Parc<- data_final_well_being %>%
    ggplot(aes(y=anxdep,x=factor(parc)))+
    geom_boxplot(aes(fill=as.factor(parc)))+
    labs(title = "Nivel de ansiedad y depresión",
         subtitle = "Según Frecuencia parque",
         y="Nivel de ansiedad y depresión",
         x= "Frecuencia con la cual la persona visita el parque",
         fill="Frec deporte")+
    scale_fill_brewer(palette = "RdPu")+
    theme_classic()
  
  
  ggsave(plot = SBS_Boxplot_Anxdep_Parc, 
         filename = "./Graficas_Definitivas/SBS_Boxplot_Anxdep_Parc.png", 
         width = 11, height = 7,
         type= "cairo",
         dpi = "retina")
  
  
  ###SHS
  
  #Side by Sied Boxplots (SHS_según Sport) 
  
  SBS_Boxplot_1<-data_final_well_being %>%
    ggplot(aes(y=SHS,x=factor(sport)))+
    geom_boxplot(aes(fill=as.factor(sport)))+
    labs(title = "Nivel de ansiedad y depresión",
         subtitle = "Según Frecuencia deporte",
         y="Nivel de felicidad",
         x= "Frecuencia con la cual la persona practica deporte",
         fill="Frec deporte")+
    scale_fill_brewer(palette = "RdPu")+
    theme_classic()
  
  
  #Export
  
  SBS_Boxplot_SHS_Sport<-data_final_well_being %>%
    ggplot(aes(y=SHS,x=factor(sport)))+
    geom_boxplot(aes(fill=as.factor(sport)))+
    labs(title = "Nivel de ansiedad y depresión",
         subtitle = "Según Frecuencia deporte",
         y="Nivel de felicidad",
         x= "Frecuencia con la cual la persona practica deporte",
         fill="Frec deporte")+
    scale_fill_brewer(palette = "RdPu")+
    theme_classic()
  
  ggsave(plot = SBS_Boxplot_SHS_Sport, 
         filename = "./Graficas_Definitivas/SBS_Boxplot_SHS_Sport.png", 
         width = 11, height = 7,
         type= "cairo",
         dpi = "retina")
  
  
  
  #Side by Sied Boxplots (SHS según Parc) 
  
  data_final_well_being %>%
    ggplot(aes(y=SHS,x=factor(parc)))+
    geom_boxplot(aes(fill=as.factor(parc)))+
    labs(title = "Nivel de ansiedad y depresión",
         subtitle = "Según Frecuencia parque",
         y="Nivel de felicidad",
         x= "Frecuencia con la cual la persona visita el parque",
         fill="Frec deporte")+
    scale_fill_brewer(palette = "RdPu")+
    theme_classic()

  #Export
  
  SBS_Boxplot_SHS_Parc<- data_final_well_being %>%
    ggplot(aes(y=SHS,x=factor(parc)))+
    geom_boxplot(aes(fill=as.factor(parc)))+
    labs(title = "Nivel de ansiedad y depresión",
         subtitle = "Según Frecuencia parque",
         y="Nivel de felicidad",
         x= "Frecuencia con la cual la persona visita el parque",
         fill="Frec deporte")+
    scale_fill_brewer(palette = "RdPu")+
    theme_classic()
  
  
  ggsave(plot = SBS_Boxplot_SHS_Parc, 
         filename = "./Graficas_Definitivas/SBS_Boxplot_SHS_Parc.png", 
         width = 11, height = 7,
         type= "cairo",
         dpi = "retina")
  

  
## Scatterplots 

#Básico

data_final_well_being %>% 
  ggplot(aes(y=SHS,x=SES))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="blue")+
  labs(title="Nivel de felicidad vs Estatus socio-económico")
## `geom_smooth()` using formula 'y ~ x'

#Con facet_grid según Sport

data_final_well_being %>% 
  ggplot(aes(y=SHS,x=anxdep,color=factor(sport)))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="dodgerblue4")+
  labs(title="Nivel de felicidad vs Ansiedad y depresión", 
       subtitle="Según Frecuencia Deporte",color="Frecuencia deporte")+
  facet_grid( ~sport)+
  scale_color_brewer(palette="Dark2")+
  theme_minimal()
## `geom_smooth()` using formula 'y ~ x'

  #Export 

  SHS_Anxdep_Facetgrid_Sport<-data_final_well_being %>% 
  ggplot(aes(y=SHS,x=anxdep,color=factor(sport)))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="dodgerblue4")+
  labs(title="Nivel de felicidad vs Ansiedad y depresión", 
       subtitle="Según Frecuencia Deporte",color="Frecuencia deporte")+
  facet_grid( ~sport)+
  scale_color_brewer(palette="Dark2")+
    theme_minimal()


  ggsave(plot = SHS_Anxdep_Facetgrid_Sport, 
       filename = "./Graficas_Definitivas/SHS_Anxdep_Facetgrid_Sport.png", 
       width = 14, height = 7,
       type= "cairo",
       dpi = "retina")
## `geom_smooth()` using formula 'y ~ x'
#Con facet_grid según Parc

data_final_well_being %>% 
  ggplot(aes(y=SHS,x=anxdep,color=factor(parc)))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="dodgerblue4")+
  labs(title="Nivel de felicidad vs Ansiedad y depresión", 
       subtitle="Según frecuencia parque",color="Frecuencia parque")+
  facet_grid( ~parc)+
  scale_color_brewer(palette="Dark2")+
  theme_minimal()
## `geom_smooth()` using formula 'y ~ x'

  #Export

  SHS_Anxdep_Facetgrid_Parc<- data_final_well_being %>% 
  ggplot(aes(y=SHS,x=anxdep,color=factor(parc)))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="dodgerblue4")+
  labs(title="Nivel de felicidad vs Ansiedad y depresión", 
       subtitle="Según frecuencia parque",color="Frecuencia parque")+
  facet_grid( ~parc)+
  scale_color_brewer(palette="Dark2")+
    theme_minimal()
  
  
  ggsave(plot = SHS_Anxdep_Facetgrid_Parc, 
         filename = "./Graficas_Definitivas/SHS_Anxdep_Facetgrid_Parc.png", 
         width = 14, height = 7,
         type= "cairo",
         dpi = "retina")
## `geom_smooth()` using formula 'y ~ x'
###Side-By-Side Scatterplots con SHS. Usando Ggarrange (paquete egg) 

A<-data_final_well_being %>% 
  ggplot(aes(y=SHS,x=SES))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="blue")+
  labs(title="Nivel de felicidad vs Estatus socio-económico")+
  theme_classic()


B <- data_final_well_being %>% 
  ggplot(aes(y=SHS,x=green))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="blue")+
  labs(title="Nivel de felicidad vs Espacio Verde Disponible")+
  theme_classic()

C<- data_final_well_being %>% 
  ggplot(aes(y=SHS,x=sport))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="blue")+
  labs(title="Nivel de felicidad vs Frecuencia deporte")+
  theme_classic()

D<- data_final_well_being %>% 
  ggplot(aes(y=SHS,x=space))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="blue")+
  labs(title="Nivel de felicidad vs Space")+
  theme_classic()

E<- data_final_well_being %>% 
  ggplot(aes(y=SHS,x=parc))+
  geom_point()+
  geom_smooth(method=lm, color="darkred", fill="blue")+
  labs(title="Nivel de felicidad vs parque")+
  theme_classic()


ggarrange(A,B,C,D,E, ncol = 3, nrow = 2)
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## adding dummy grobs

  #Export

  SBS_Scatterplot_SHS<-ggarrange(A,B,C,D,E, ncol = 3, nrow = 2)
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## adding dummy grobs

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

  
#########
  
  ##Anxdep

  ###Side-By-Side Scatterplots con Anxdep. Usando Ggarrange (paquete egg) 
  
  A.1<-data_final_well_being %>% 
    ggplot(aes(y=anxdep,x=SES))+
    geom_point()+
    geom_smooth(method=lm, color="darkred", fill="blue")+
    labs(title="Nivel de ansiedad y derpesión vs Estatus socio-económico")+
    theme_classic()
  
  
  B.1 <- data_final_well_being %>% 
    ggplot(aes(y=anxdep,x=green))+
    geom_point()+
    geom_smooth(method=lm, color="darkred", fill="blue")+
    labs(title="Nivel de ansiedad y depresión vs Espacio Verde Disponible")+
    theme_classic()
  
  C.1<- data_final_well_being %>% 
    ggplot(aes(y=anxdep,x=sport))+
    geom_point()+
    geom_smooth(method=lm, color="darkred", fill="blue")+
    labs(title="Nivel de ansiedad y depresión vs Frecuencia deporte")+
    theme_classic()
  
  D.1<- data_final_well_being %>% 
    ggplot(aes(y=anxdep,x=space))+
    geom_point()+
    geom_smooth(method=lm, color="darkred", fill="blue")+
    labs(title="Nivel de ansiedad y depresión vs Space")+
    theme_classic()
  
  E.1<- data_final_well_being %>% 
    ggplot(aes(y=anxdep,x=parc))+
    geom_point()+
    geom_smooth(method=lm, color="darkred", fill="blue")+
    labs(title="Nivel de ansiedad y depresión vs parque")+
    theme_classic()
  
  
  ggarrange(A.1,B.1,C.1,D.1,E.1, ncol = 3, nrow = 2)
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## adding dummy grobs

  #Export
  
  SBS_Scatterplot_Anxdep<-  ggarrange(A.1,B.1,C.1,D.1,E.1, ncol = 3, nrow = 2)
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## `geom_smooth()` using formula 'y ~ x'
## adding dummy grobs

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

7 Regresiones lineales:

Dos variables dependientes. Dos modelos distitnos

7.0.1 Modelo 1. Variable dependiente: “SHS”

#Modelo completo (M1 full)

m1_SHS_Full<-lm(SHS ~ green +
         space +
          parc +
          SES+
          sport,
       data = data_final_well_being)

summary(m1_SHS_Full)
## 
## Call:
## lm(formula = SHS ~ green + space + parc + SES + sport, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.0327 -0.9563 -0.2233  0.8705  2.9068 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.400842   0.394484   8.621 3.63e-16 ***
## green       -0.378131   0.560258  -0.675  0.50023    
## space        0.058082   0.108951   0.533  0.59435    
## parc         0.029350   0.114742   0.256  0.79828    
## SES          0.007062   0.004743   1.489  0.13751    
## sport        0.142521   0.051413   2.772  0.00591 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.3 on 306 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.05832,    Adjusted R-squared:  0.04293 
## F-statistic:  3.79 on 5 and 306 DF,  p-value: 0.002405

M1. Regresiones Simples por cada variable dependiente

 #Green
  m1_SHS_Full_Green<-lm(SHS ~ green,
                data = data_final_well_being)

    summary(m1_SHS_Full_Green)
## 
## Call:
## lm(formula = SHS ~ green, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.7725 -1.0189 -0.2581  0.9680  2.5027 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   4.4925     0.1485  30.246   <2e-16 ***
## green         0.1198     0.4935   0.243    0.808    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.328 on 313 degrees of freedom
## Multiple R-squared:  0.0001884,  Adjusted R-squared:  -0.003006 
## F-statistic: 0.05899 on 1 and 313 DF,  p-value: 0.8083
  #Space
    m1_SHS_Full_Space<-lm(SHS ~ space,
                          data = data_final_well_being)
    
    summary(m1_SHS_Full_Space)
## 
## Call:
## lm(formula = SHS ~ space, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8710 -1.0064 -0.2374  0.9936  2.5508 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   4.3919     0.1483  29.613   <2e-16 ***
## space         0.1145     0.1096   1.045    0.297    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.329 on 310 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.003511,   Adjusted R-squared:  0.0002963 
## F-statistic: 1.092 on 1 and 310 DF,  p-value: 0.2968
  #Parc
    m1_SHS_Full_Parc<-lm(SHS ~ parc,
                          data = data_final_well_being)
    
    summary(m1_SHS_Full_Parc)
## 
## Call:
## lm(formula = SHS ~ parc, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.7837 -1.0347 -0.2847  0.9643  2.7143 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.03670    0.18986  21.262  < 2e-16 ***
## parc         0.24901    0.08942   2.785  0.00568 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.312 on 313 degrees of freedom
## Multiple R-squared:  0.02418,    Adjusted R-squared:  0.02106 
## F-statistic: 7.754 on 1 and 313 DF,  p-value: 0.005684
  #SES
    m1_SHS_Full_SES<-lm(SHS ~ SES,
                         data = data_final_well_being)
    
    summary(m1_SHS_Full_SES)
## 
## Call:
## lm(formula = SHS ~ SES, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9718 -0.9256 -0.2218  0.9475  2.6308 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 3.924978   0.359890   10.91   <2e-16 ***
## SES         0.007051   0.004147    1.70   0.0901 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.322 on 313 degrees of freedom
## Multiple R-squared:  0.009152,   Adjusted R-squared:  0.005987 
## F-statistic: 2.891 on 1 and 313 DF,  p-value: 0.09006
  #Sport
    m1_SHS_Full_Sport<-lm(SHS ~ sport,
                        data = data_final_well_being)
    
    summary(m1_SHS_Full_Sport)
## 
## Call:
## lm(formula = SHS ~ sport, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.0145 -0.9477 -0.1977  0.8691  2.8691 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.97254    0.15522  25.594  < 2e-16 ***
## sport        0.15839    0.03937   4.023 7.21e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.295 on 313 degrees of freedom
## Multiple R-squared:  0.04917,    Adjusted R-squared:  0.04613 
## F-statistic: 16.19 on 1 and 313 DF,  p-value: 7.206e-05

Uniendo todos los modelos (m1) en una sola tabla

stargazer(m1_SHS_Full_Green,
              m1_SHS_Full_Parc,
              m1_SHS_Full_SES,
              m1_SHS_Full_Space,
              m1_SHS_Full_Sport,
              m1_SHS_Full,
              type = "text",
              title="Nivel de felicidad",
              align=TRUE)    
## 
## Nivel de felicidad
## ======================================================================================================================================================
##                                                                            Dependent variable:                                                        
##                     ----------------------------------------------------------------------------------------------------------------------------------
##                                                                                    SHS                                                                
##                             (1)                  (2)                   (3)                  (4)                   (5)                    (6)          
## ------------------------------------------------------------------------------------------------------------------------------------------------------
## green                      0.120                                                                                                        -0.378        
##                           (0.493)                                                                                                      (0.560)        
##                                                                                                                                                       
## parc                                           0.249***                                                                                 0.029         
##                                                (0.089)                                                                                 (0.115)        
##                                                                                                                                                       
## SES                                                                   0.007*                                                            0.007         
##                                                                      (0.004)                                                           (0.005)        
##                                                                                                                                                       
## space                                                                                      0.115                                        0.058         
##                                                                                           (0.110)                                      (0.109)        
##                                                                                                                                                       
## sport                                                                                                          0.158***                0.143***       
##                                                                                                                 (0.039)                (0.051)        
##                                                                                                                                                       
## Constant                 4.492***              4.037***              3.925***            4.392***              3.973***                3.401***       
##                           (0.149)              (0.190)               (0.360)              (0.148)               (0.155)                (0.394)        
##                                                                                                                                                       
## ------------------------------------------------------------------------------------------------------------------------------------------------------
## Observations                315                  315                   315                  312                   315                    312          
## R2                        0.0002                0.024                 0.009                0.004                 0.049                  0.058         
## Adjusted R2               -0.003                0.021                 0.006               0.0003                 0.046                  0.043         
## Residual Std. Error  1.328 (df = 313)      1.312 (df = 313)      1.322 (df = 313)    1.329 (df = 310)      1.295 (df = 313)        1.300 (df = 306)   
## F Statistic         0.059 (df = 1; 313) 7.754*** (df = 1; 313) 2.891* (df = 1; 313) 1.092 (df = 1; 310) 16.186*** (df = 1; 313) 3.790*** (df = 5; 306)
## ======================================================================================================================================================
## Note:                                                                                                                      *p<0.1; **p<0.05; ***p<0.01
  #Export   

  stargazer(m1_SHS_Full_Green,
          m1_SHS_Full_Parc,
          m1_SHS_Full_SES,
          m1_SHS_Full_Space,
          m1_SHS_Full_Sport,
          m1_SHS_Full,
          type = "html",
          title="Nivel de felicidad",
          align=TRUE,
          out = "Regression_Tables/SBS_m1_SHS.html")

8 Modelo 1. Variable dependiente: “anxdep”

#Modelo completo (M2 full)

m2_Anxdep_Full<-lm(anxdep ~ green +
         space +
         parc +
         SES+
         sport,
       data = data_final_well_being)

summary(m2_Anxdep_Full)
## 
## Call:
## lm(formula = anxdep ~ green + space + parc + SES + sport, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.9913 -0.4431 -0.1043  0.3590  2.1832 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.365904   0.181662  13.024   <2e-16 ***
## green        0.046157   0.258002   0.179   0.8581    
## space       -0.043309   0.050173  -0.863   0.3887    
## parc        -0.103239   0.052840  -1.954   0.0516 .  
## SES         -0.003037   0.002184  -1.391   0.1653    
## sport       -0.009477   0.023676  -0.400   0.6892    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5987 on 306 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.04143,    Adjusted R-squared:  0.02577 
## F-statistic: 2.645 on 5 and 306 DF,  p-value: 0.02328

M2. Regresiones Simples

  #Green
    m2_Anxdep_Full_Green<-lm(anxdep ~ green,
                      data = data_final_well_being)

   summary(m2_Anxdep_Full_Green)
## 
## Call:
## lm(formula = anxdep ~ green, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.8840 -0.4889 -0.1081  0.3884  2.0751 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.89631    0.06859  27.646   <2e-16 ***
## green       -0.20599    0.22788  -0.904    0.367    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6134 on 313 degrees of freedom
## Multiple R-squared:  0.002604,   Adjusted R-squared:  -0.0005827 
## F-statistic: 0.8171 on 1 and 313 DF,  p-value: 0.3667
  #Space
    m2_Anxdep_Full_Space<-lm(anxdep ~ space,
                      data = data_final_well_being)

    summary(m2_Anxdep_Full_Space)
## 
## Call:
## lm(formula = anxdep ~ space, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.8810 -0.4788 -0.1090  0.3826  2.0817 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.91590    0.06760  28.342   <2e-16 ***
## space       -0.06980    0.04996  -1.397    0.163    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6056 on 310 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.006258,   Adjusted R-squared:  0.003052 
## F-statistic: 1.952 on 1 and 310 DF,  p-value: 0.1634
  #Parc
    m2_Anxdep_Full_Parc<-lm(anxdep ~ parc,
                     data = data_final_well_being)

    summary(m2_Anxdep_Full_Parc)
## 
## Call:
## lm(formula = anxdep ~ parc, data = data_final_well_being)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.96395 -0.48288 -0.08356  0.36877  2.07796 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.09079    0.08755  23.881  < 2e-16 ***
## parc        -0.12684    0.04124  -3.076  0.00228 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6052 on 313 degrees of freedom
## Multiple R-squared:  0.02934,    Adjusted R-squared:  0.02624 
## F-statistic: 9.461 on 1 and 313 DF,  p-value: 0.002284
  #SES
    m2_Anxdep_Full_SES<-lm(anxdep ~ SES,
                    data = data_final_well_being)

    summary(m2_Anxdep_Full_SES)
## 
## Call:
## lm(formula = anxdep ~ SES, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.8964 -0.4705 -0.1069  0.3667  2.0173 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.129471   0.166344  12.802   <2e-16 ***
## SES         -0.003377   0.001917  -1.762   0.0791 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6112 on 313 degrees of freedom
## Multiple R-squared:  0.00982,    Adjusted R-squared:  0.006656 
## F-statistic: 3.104 on 1 and 313 DF,  p-value: 0.07907
  #Sport
    m2_Anxdep_Full_Sport<-lm(anxdep ~ sport,
                      data = data_final_well_being)

    summary(m2_Anxdep_Full_Sport)
## 
## Call:
## lm(formula = anxdep ~ sport, data = data_final_well_being)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.9606 -0.4704 -0.1229  0.3423  2.0653 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.00819    0.07283  27.573   <2e-16 ***
## sport       -0.04755    0.01847  -2.574   0.0105 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6078 on 313 degrees of freedom
## Multiple R-squared:  0.02073,    Adjusted R-squared:  0.0176 
## F-statistic: 6.625 on 1 and 313 DF,  p-value: 0.01052

Uniendo todos los modelos (m2) en una sola tabla

stargazer(m2_Anxdep_Full_Green,
              m2_Anxdep_Full_Parc,
              m2_Anxdep_Full_SES,
              m2_Anxdep_Full_Space,
              m2_Anxdep_Full_Sport,
              m2_Anxdep_Full,
              type = "text",
              title="Nivel de ansiedad y depresión",
              align=TRUE)
## 
## Nivel de ansiedad y depresión
## ===================================================================================================================================================
##                                                                           Dependent variable:                                                      
##                     -------------------------------------------------------------------------------------------------------------------------------
##                                                                                 anxdep                                                             
##                             (1)                  (2)                   (3)                  (4)                  (5)                   (6)         
## ---------------------------------------------------------------------------------------------------------------------------------------------------
## green                     -0.206                                                                                                      0.046        
##                           (0.228)                                                                                                    (0.258)       
##                                                                                                                                                    
## parc                                          -0.127***                                                                              -0.103*       
##                                                (0.041)                                                                               (0.053)       
##                                                                                                                                                    
## SES                                                                  -0.003*                                                         -0.003        
##                                                                      (0.002)                                                         (0.002)       
##                                                                                                                                                    
## space                                                                                     -0.070                                     -0.043        
##                                                                                           (0.050)                                    (0.050)       
##                                                                                                                                                    
## sport                                                                                                         -0.048**               -0.009        
##                                                                                                                (0.018)               (0.024)       
##                                                                                                                                                    
## Constant                 1.896***              2.091***              2.129***            1.916***             2.008***              2.366***       
##                           (0.069)              (0.088)               (0.166)              (0.068)              (0.073)               (0.182)       
##                                                                                                                                                    
## ---------------------------------------------------------------------------------------------------------------------------------------------------
## Observations                315                  315                   315                  312                  315                   312         
## R2                         0.003                0.029                 0.010                0.006                0.021                 0.041        
## Adjusted R2               -0.001                0.026                 0.007                0.003                0.018                 0.026        
## Residual Std. Error  0.613 (df = 313)      0.605 (df = 313)      0.611 (df = 313)    0.606 (df = 310)     0.608 (df = 313)      0.599 (df = 306)   
## F Statistic         0.817 (df = 1; 313) 9.461*** (df = 1; 313) 3.104* (df = 1; 313) 1.952 (df = 1; 310) 6.625** (df = 1; 313) 2.645** (df = 5; 306)
## ===================================================================================================================================================
## Note:                                                                                                                   *p<0.1; **p<0.05; ***p<0.01
  #Export

  stargazer(m2_Anxdep_Full_Green,
          m2_Anxdep_Full_Parc,
          m2_Anxdep_Full_SES,
          m2_Anxdep_Full_Space,
          m2_Anxdep_Full_Sport,
          m2_Anxdep_Full,
          type = "html",
          title="Nivel de ansiedad y depresión",
          align=TRUE,
          out = "Regression_Tables/SBS_m2_Anxdep.html")

9 Inference

#Usando la función Inference
  
  #Hypothesis test
  inference(data=data_final_well_being, y = anxdep, statistic = "mean", type = "ht", 
              null = 0, alternative = "twosided", method = "theoretical")
## Single numerical variable
## n = 315, y-bar = 1.8427, s = 0.6133
## H0: mu = 0
## HA: mu != 0
## t = 53.3304, df = 314
## p_value = < 0.0001

  #Confidence Interval
  inference(data=data_final_well_being, y = anxdep, statistic = "mean", type = "ci", 
              null = 0, alternative = "twosided", method = "theoretical")
## Single numerical variable
## n = 315, y-bar = 1.8427, s = 0.6133
## 95% CI: (1.7748 , 1.9107)

  t.test(x=data_final_well_being$anxdep)
## 
##  One Sample t-test
## 
## data:  data_final_well_being$anxdep
## t = 53.33, df = 314, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  1.774763 1.910734
## sample estimates:
## mean of x 
##  1.842748