library(ggplot2)
library(ggeffects)
library(tidyr)
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(haven)
library(naniar)
library(report)
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
Egoproject<-read.csv("Parental Egocentrism Project.csv") #Loading in data file
Sex <- factor(Egoproject$chsex_d1, levels = 1:2, labels = c("Male", "Female")) #Turning sex into a factor
summary(Sex) #Getting summary of sex
##   Male Female 
##    111    109
Ethnicity <- factor(Egoproject$cethn_d1, levels = 1:6, labels = c("White", "Black", "Hispanic", "Asian-Oriental", "Mixed", "Other")) #Turning ethnicity into a factor
summary(Ethnicity) #Getting summary of ethnicity 
##          White          Black       Hispanic Asian-Oriental          Mixed 
##            154             35              4              6             21 
##          Other 
##              0
Income <- factor(Egoproject$inc_d1, levels = 1:6, labels = c("Less than $20k", "$20k-40k", "$41k-$60k", "$61k-$80k", "$81k-$100k", "Over $100k")) #Turning income into a factor
summary(Income) #Getting summary of income 
## Less than $20k       $20k-40k      $41k-$60k      $61k-$80k     $81k-$100k 
##             12             25             40             41             35 
##     Over $100k           NA's 
##             66              1
mean(Egoproject$cage_d1) #Calculating adolescents' mean age
## [1] 13.67034
sd(Egoproject$cage_d1) #Calculating adolescents' age sd
## [1] 1.520727
Ego <- subset(Egoproject,select=c(id,chsex_d1, cage_d1, cethn_d1, inc_d1, t_dep_m1, t_dep_f1, ego_m, ego_f, temo_c1)) #Designating columns to run analyses on
vis_miss(Ego) #Looking at missing data
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.

Ego <- na.omit(Ego) #Excluding missing data 
r <- corr.test(Ego$t_dep_f1,Ego$ego_f)
print(r,short=F) #Calculating pearson's correlation r for fathers' depression and egocentrism 
## Call:corr.test(x = Ego$t_dep_f1, y = Ego$ego_f)
## Correlation matrix 
## [1] 0.19
## Sample Size 
## [1] 152
## These are the unadjusted probability values.
##   The probability values  adjusted for multiple tests are in the p.adj object. 
## [1] 0.02
## 
##  Confidence intervals based upon normal theory.  To get bootstrapped values, try cor.ci
##       raw.lower raw.r raw.upper raw.p lower.adj upper.adj
## NA-NA      0.03  0.19      0.34  0.02      0.03      0.34
ggplot(data = Ego, aes(x =t_dep_f1,y=ego_f)) + 
  geom_point() +
  xlab("Paternal Depression") +
  ylab("Paternal Egocentrism") +
  geom_smooth(method = "lm") #Plotting correlation for fathers' depression and egocentrism  using a scatter plot
## `geom_smooth()` using formula 'y ~ x'

Ego$ego_f1c<- scale(Ego$ego_f,center=T,scale=F)
Ego$temo_c1c <- scale(Ego$temo_c1,center=T,scale=F) #Mean centering fathers' egocentrism and adolescents' emotion-focused coping
reg <- lm(temo_c1~ego_f,data=Ego)
summary(reg) #Computing regression for fathers' egocentrism and adolescents' emotion-focused coping
## 
## Call:
## lm(formula = temo_c1 ~ ego_f, data = Ego)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -23.5260  -6.7891  -0.4733   6.9214  26.7372 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  45.8417     1.6812  27.267   <2e-16 ***
## ego_f         1.4211     0.7011   2.027   0.0444 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.33 on 150 degrees of freedom
## Multiple R-squared:  0.02666,    Adjusted R-squared:  0.02017 
## F-statistic: 4.108 on 1 and 150 DF,  p-value: 0.04445
confint(reg) #Finding CI of mean centered regression
##                   2.5 %    97.5 %
## (Intercept) 42.51978564 49.163653
## ego_f        0.03572971  2.806414
report(reg)
## We fitted a linear model (estimated using OLS) to predict temo_c1 with ego_f
## (formula: temo_c1 ~ ego_f). The model explains a statistically significant and
## weak proportion of variance (R2 = 0.03, F(1, 150) = 4.11, p = 0.044, adj. R2 =
## 0.02). The model's intercept, corresponding to ego_f = 0, is at 45.84 (95% CI
## [42.52, 49.16], t(150) = 27.27, p < .001). Within this model:
## 
##   - The effect of ego f is statistically significant and positive (beta = 1.42,
## 95% CI [0.04, 2.81], t(150) = 2.03, p = 0.044; Std. beta = 0.16, 95% CI
## [4.11e-03, 0.32])
## 
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
gg <- ggpredict(reg,terms=c("ego_f"))

plot(gg)+labs(x="Paternal Egocentrism",y= "Adolescent Emotion-Focused Coping",title="") #Plotting regression between fathers' egocentrism and adolescnts' emotion-focused coping

describe(Ego$chsex_d1)
##    vars   n mean  sd median trimmed mad min max range skew kurtosis   se
## X1    1 152 1.49 0.5      1    1.49   0   1   2     1 0.03    -2.01 0.04
describe(Ego$t_dep_f1) #Computing M and SD of adolescents' sex and fathers' depression
##    vars   n mean    sd median trimmed   mad min max range skew kurtosis   se
## X1    1 152 54.8 10.65     54   54.52 11.12  38  80    42  0.2    -0.51 0.86
Ego$chsex_d1 <- as.factor(Ego$chsex_d1)
levels(Ego$chsex_d1) <- list('Male'="1",'Female'="2") #Relabel adolescents' sex to ensure it is treated as a factor, not numerical
FemaleC <- (Ego$temo_c1[Ego$chsex_d1 == "Female"])
describe(FemaleC)
##    vars  n  mean    sd median trimmed   mad min max range skew kurtosis   se
## X1    1 75 48.31 10.93     48   48.26 10.38  27  71    44 0.08    -0.77 1.26
MaleC <- (Ego$temo_c1[Ego$chsex_d1 == "Male"])
describe(MaleC) #Computing M and SD of females and males' emotion-focused coping
##    vars  n  mean   sd median trimmed mad min max range skew kurtosis   se
## X1    1 77 49.27 9.98     47    49.1 8.9  28  74    46 0.22    -0.41 1.14
var.test(FemaleC,MaleC) #Looking at homogeneity of variance for adolescents' sex and emotion-focused coping
## 
##  F test to compare two variances
## 
## data:  FemaleC and MaleC
## F = 1.1986, num df = 74, denom df = 76, p-value = 0.4339
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.7601059 1.8926373
## sample estimates:
## ratio of variances 
##           1.198577
t.test(FemaleC, MaleC,var.equal=T) #Conducting a two sample t-test between adolescents' sex and emotion-focused coping
## 
##  Two Sample t-test
## 
## data:  FemaleC and MaleC
## t = -0.56936, df = 150, p-value = 0.57
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -4.318676  2.386555
## sample estimates:
## mean of x mean of y 
##  48.30667  49.27273
model1 <- t.test(FemaleC, MaleC,var.equal=T) 
print(model1)
## 
##  Two Sample t-test
## 
## data:  FemaleC and MaleC
## t = -0.56936, df = 150, p-value = 0.57
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -4.318676  2.386555
## sample estimates:
## mean of x mean of y 
##  48.30667  49.27273
report(model1)
## Warning: Missing values detected. NAs dropped.
## Effect sizes were labelled following Cohen's (1988) recommendations.
## 
## The Two Sample t-test testing the difference between FemaleC and MaleC (mean of
## x = 48.31, mean of y = 49.27) suggests that the effect is negative,
## statistically not significant, and very small (difference = -0.97, 95% CI
## [-4.32, 2.39], t(150) = -0.57, p = 0.570; Cohen's d = -0.09, 95% CI [-0.41,
## 0.23])
cohens_d(model1) #Computing cohen's d for two sample t-test
## Warning: Missing values detected. NAs dropped.
## Cohen's d |        95% CI
## -------------------------
## -0.09     | [-0.41, 0.23]
## 
## - Estimated using pooled SD.
dfSumm <- Ego %>%
  group_by(chsex_d1) %>%
  dplyr:: summarise(
    n=n(), 
    sd = sd(temo_c1, na.rm=T), 
    temo_c1 = mean(temo_c1,na.rm=T) 
    )%>%
  mutate( se=sd/sqrt(n))  %>% 
  mutate( ci=se * 1.96) 
            
ggplot(dfSumm, aes(chsex_d1, temo_c1)) +
  geom_col(fill=c("blue", "red")) +
  geom_errorbar(aes(ymin = temo_c1-ci, ymax = temo_c1+ci),width=0.3)+
  xlab("Adolescents' Sex")+
  ylab("Adolescents' Emotion-Focused Coping") #Graphing two samples t-test between adolescents' sex and emotion-focused coping

sessionInfo()
## R version 4.2.2 (2022-10-31)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Ventura 13.0
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] effectsize_0.8.2 report_0.5.5     naniar_0.6.1     haven_2.5.1     
##  [5] dplyr_1.0.10     Rmisc_1.5.1      plyr_1.8.7       lattice_0.20-45 
##  [9] psych_2.2.5      tidyr_1.2.1      ggeffects_1.1.4  ggplot2_3.3.6   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.9         assertthat_0.2.1   digest_0.6.29      utf8_1.2.2        
##  [5] R6_2.5.1           visdat_0.5.3       evaluate_0.16      highr_0.9         
##  [9] pillar_1.8.1       rlang_1.0.5        performance_0.10.0 rstudioapi_0.14   
## [13] jquerylib_0.1.4    Matrix_1.5-1       rmarkdown_2.16     splines_4.2.2     
## [17] labeling_0.4.2     stringr_1.4.1      munsell_0.5.0      compiler_4.2.2    
## [21] xfun_0.33          pkgconfig_2.0.3    parameters_0.19.0  mnormt_2.1.0      
## [25] mgcv_1.8-41        htmltools_0.5.3    insight_0.18.6     tidyselect_1.1.2  
## [29] tibble_3.1.8       fansi_1.0.3        withr_2.5.0        grid_4.2.2        
## [33] nlme_3.1-160       jsonlite_1.8.0     gtable_0.3.1       lifecycle_1.0.2   
## [37] DBI_1.1.3          magrittr_2.0.3     bayestestR_0.13.0  scales_1.2.1      
## [41] datawizard_0.6.3   cli_3.4.0          stringi_1.7.8      cachem_1.0.6      
## [45] farver_2.1.1       bslib_0.4.0        ellipsis_0.3.2     generics_0.1.3    
## [49] vctrs_0.4.1        tools_4.2.2        forcats_0.5.2      glue_1.6.2        
## [53] purrr_0.3.4        hms_1.1.2          parallel_4.2.2     fastmap_1.1.0     
## [57] yaml_2.3.5         colorspace_2.0-3   knitr_1.40         sass_0.4.2
citation("ggplot2")
## 
## To cite ggplot2 in publications, please use:
## 
##   H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
##   Springer-Verlag New York, 2016.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Book{,
##     author = {Hadley Wickham},
##     title = {ggplot2: Elegant Graphics for Data Analysis},
##     publisher = {Springer-Verlag New York},
##     year = {2016},
##     isbn = {978-3-319-24277-4},
##     url = {https://ggplot2.tidyverse.org},
##   }
citation("ggeffects")
## 
## To cite package 'ggeffects' in publications use:
## 
##   Lüdecke D (2018). "ggeffects: Tidy Data Frames of Marginal Effects
##   from Regression Models." _Journal of Open Source Software_, *3*(26),
##   772. doi:10.21105/joss.00772 <https://doi.org/10.21105/joss.00772>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Article{,
##     title = {ggeffects: Tidy Data Frames of Marginal Effects from Regression Models.},
##     volume = {3},
##     doi = {10.21105/joss.00772},
##     number = {26},
##     journal = {Journal of Open Source Software},
##     author = {Daniel Lüdecke},
##     year = {2018},
##     pages = {772},
##   }
citation("tidyr")
## 
## To cite package 'tidyr' in publications use:
## 
##   Wickham H, Girlich M (2022). _tidyr: Tidy Messy Data_. R package
##   version 1.2.1, <https://CRAN.R-project.org/package=tidyr>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {tidyr: Tidy Messy Data},
##     author = {Hadley Wickham and Maximilian Girlich},
##     year = {2022},
##     note = {R package version 1.2.1},
##     url = {https://CRAN.R-project.org/package=tidyr},
##   }
citation("psych")
## 
## To cite the psych package in publications use:
## 
##   Revelle, W. (2022) psych: Procedures for Personality and
##   Psychological Research, Northwestern University, Evanston, Illinois,
##   USA, https://CRAN.R-project.org/package=psych Version = 2.2.5.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {psych: Procedures for Psychological, Psychometric, and Personality Research},
##     author = {William Revelle},
##     organization = { Northwestern University},
##     address = { Evanston, Illinois},
##     year = {2022},
##     note = {R package version 2.2.5},
##     url = {https://CRAN.R-project.org/package=psych},
##   }
citation("Rmisc")
## 
## To cite package 'Rmisc' in publications use:
## 
##   Hope RM (2022). _Rmisc: Ryan Miscellaneous_. R package version 1.5.1,
##   <https://CRAN.R-project.org/package=Rmisc>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {Rmisc: Ryan Miscellaneous},
##     author = {Ryan M. Hope},
##     year = {2022},
##     note = {R package version 1.5.1},
##     url = {https://CRAN.R-project.org/package=Rmisc},
##   }
## 
## ATTENTION: This citation information has been auto-generated from the
## package DESCRIPTION file and may need manual editing, see
## 'help("citation")'.
citation("dplyr")
## 
## To cite package 'dplyr' in publications use:
## 
##   Wickham H, François R, Henry L, Müller K (2022). _dplyr: A Grammar of
##   Data Manipulation_. R package version 1.0.10,
##   <https://CRAN.R-project.org/package=dplyr>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {dplyr: A Grammar of Data Manipulation},
##     author = {Hadley Wickham and Romain François and Lionel Henry and Kirill Müller},
##     year = {2022},
##     note = {R package version 1.0.10},
##     url = {https://CRAN.R-project.org/package=dplyr},
##   }
citation("haven")
## 
## To cite package 'haven' in publications use:
## 
##   Wickham H, Miller E, Smith D (2022). _haven: Import and Export
##   'SPSS', 'Stata' and 'SAS' Files_. R package version 2.5.1,
##   <https://CRAN.R-project.org/package=haven>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {haven: Import and Export 'SPSS', 'Stata' and 'SAS' Files},
##     author = {Hadley Wickham and Evan Miller and Danny Smith},
##     year = {2022},
##     note = {R package version 2.5.1},
##     url = {https://CRAN.R-project.org/package=haven},
##   }
citation("naniar")
## 
## To cite package 'naniar' in publications use:
## 
##   Tierney N, Cook D, McBain M, Fay C (2021). _naniar: Data Structures,
##   Summaries, and Visualisations for Missing Data_. R package version
##   0.6.1, <https://CRAN.R-project.org/package=naniar>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {naniar: Data Structures, Summaries, and Visualisations for Missing Data},
##     author = {Nicholas Tierney and Di Cook and Miles McBain and Colin Fay},
##     year = {2021},
##     note = {R package version 0.6.1},
##     url = {https://CRAN.R-project.org/package=naniar},
##   }
citation("report")
## 
## To cite in publications use:
## 
##   Makowski, D., Ben-Shachar, M.S., Patil, I. & Lüdecke, D. (2020).
##   Automated Results Reporting as a Practical Tool to Improve
##   Reproducibility and Methodological Best Practices Adoption. CRAN.
##   Available from https://github.com/easystats/report. doi: .
## 
## A BibTeX entry for LaTeX users is
## 
##   @Article{,
##     title = {Automated Results Reporting as a Practical Tool to Improve Reproducibility and Methodological Best Practices Adoption},
##     author = {Dominique Makowski and Mattan S. Ben-Shachar and Indrajeet Patil and Daniel Lüdecke},
##     year = {2021},
##     journal = {CRAN},
##     url = {https://github.com/easystats/report},
##   }
citation("effectsize")
## 
## To cite effectsize in publications use:
## 
##   Ben-Shachar M, Lüdecke D, Makowski D (2020). effectsize: Estimation
##   of Effect Size Indices and Standardized Parameters. Journal of Open
##   Source Software, 5(56), 2815. doi: 10.21105/joss.02815
## 
## A BibTeX entry for LaTeX users is
## 
##   @Article{,
##     title = {{e}ffectsize: Estimation of Effect Size Indices and Standardized Parameters},
##     author = {Mattan S. Ben-Shachar and Daniel Lüdecke and Dominique Makowski},
##     year = {2020},
##     journal = {Journal of Open Source Software},
##     volume = {5},
##     number = {56},
##     pages = {2815},
##     publisher = {The Open Journal},
##     doi = {10.21105/joss.02815},
##     url = {https://doi.org/10.21105/joss.02815},
##   }