library(haven)
lec<-read.csv("LEC.SEX.AGE.csv") #Loading in data file
Sex <- factor(lec$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(lec$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(lec$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(lec$cage_d1) #Calculating adolescents' mean age
## [1] 13.67034
sd(lec$cage_d1) #Calculating adolescents' age sd
## [1] 1.520727
lec2 <- subset(lec,select=c(chsex_d1, cage_d1, ltnt_c1, cethn_d1)) #Designating columns to run analyses on
library(naniar)
vis_miss(lec2) #Looking at missing data
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.

lec2 <- na.omit(lec2) #Excluding missing data
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
r <- corr.test(lec2$cage_d1,lec2$ltnt_c1)
print(r,short=F) #Calculating pearson's correlation r for adolescents' age and negative life events
## Call:corr.test(x = lec2$cage_d1, y = lec2$ltnt_c1)
## Correlation matrix
## [1] 0.12
## Sample Size
## [1] 216
## These are the unadjusted probability values.
## The probability values adjusted for multiple tests are in the p.adj object.
## [1] 0.07
##
## 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.01 0.12 0.25 0.07 -0.01 0.25
ggplot(data = lec2, aes(x = cage_d1,y=ltnt_c1)) +
geom_point() +
xlab("Adolescent Age") +
ylab("Negative Life Events") +
geom_smooth(method = "lm") #Plotting correlation for adolescents' age and negative life events using a scatter plot
## `geom_smooth()` using formula 'y ~ x'

lec2$chsex_d1 <- as.numeric(lec2$chsex_d1)
levels(lec2$chsex_d1) <- list("0"='Male',"1"='Female') #Relabel adolescents' sex to ensure it is treated as a factor, not numerical
lec2$chsex_d1 <- as.factor(lec2$chsex_d1)
levels(lec2$chsex_d1) <- list('Male'="1",'Female'="2") #Relabel adolescents' sex to ensure it is treated as a factor, not numerical
reg <- lm(cage_d1~ltnt_c1, data=lec2)
summary(reg) #Computing regression for adolescents' age and negative life events
##
## Call:
## lm(formula = cage_d1 ~ ltnt_c1, data = lec2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.7929 -1.0450 0.0982 0.9941 3.4950
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.39444 0.18491 72.439 <2e-16 ***
## ltnt_c1 0.07429 0.04113 1.806 0.0723 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.516 on 214 degrees of freedom
## Multiple R-squared: 0.01502, Adjusted R-squared: 0.01041
## F-statistic: 3.263 on 1 and 214 DF, p-value: 0.07228
confint(reg) #Finding CI
## 2.5 % 97.5 %
## (Intercept) 13.029973053 13.7589157
## ltnt_c1 -0.006779961 0.1553556
lec2$ccage_d1 <- scale(lec2$cage_d1,center=T,scale=F) #Mean centering adolescent age
reg2 <- lm(ccage_d1~ltnt_c1,data=lec2)
summary(reg2) #Computing regression for mean centered variable
##
## Call:
## lm(formula = ccage_d1 ~ ltnt_c1, data = lec2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.7929 -1.0450 0.0982 0.9941 3.4950
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.27720 0.18491 -1.499 0.1353
## ltnt_c1 0.07429 0.04113 1.806 0.0723 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.516 on 214 degrees of freedom
## Multiple R-squared: 0.01502, Adjusted R-squared: 0.01041
## F-statistic: 3.263 on 1 and 214 DF, p-value: 0.07228
confint(reg2) #Finding CI of mean centered regression
## 2.5 % 97.5 %
## (Intercept) -0.641674981 0.08726764
## ltnt_c1 -0.006779961 0.15535563
lec2$zcage_d1 <- scale(lec2$cage_d1)
lec2$zltnt_c1 <- scale(lec2$ltnt_c1) #Standardized beta coefficients for variables
reg3 <- lm(zcage_d1~zltnt_c1,data=lec2)
summary(reg3) #Computing regression for standardized variables
##
## Call:
## lm(formula = zcage_d1 ~ zltnt_c1, data = lec2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.83288 -0.68579 0.06446 0.65238 2.29360
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -7.201e-16 6.769e-02 0.000 1.0000
## zltnt_c1 1.225e-01 6.784e-02 1.806 0.0723 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9948 on 214 degrees of freedom
## Multiple R-squared: 0.01502, Adjusted R-squared: 0.01041
## F-statistic: 3.263 on 1 and 214 DF, p-value: 0.07228
gg <- ggpredict(reg2,terms=c("ccage_d1"))
## `ccage_d1` was not found in model terms. Maybe misspelled?
plot(gg)+labs(x="Adolescent Age",y= "Negative Life Events",title="") #Plotting regression between adolescents' age and negative life events

mean(lec2$ltnt_c1) #Computing mean of negative life events
## [1] 3.731481
sd (lec2$ltnt_c1) #Computing SD of negative life events
## [1] 2.513615
mean (lec2$cage_d1) #Computing mean of adolescents' age
## [1] 13.67165
sd(lec2$cage_d1) #Computing SD of adolescents' age
## [1] 1.523802
t.test(lec2$ltnt_c1,mu=2.5)#Conducting a one sample t-test for negative life events
##
## One Sample t-test
##
## data: lec2$ltnt_c1
## t = 7.2004, df = 215, p-value = 9.936e-12
## alternative hypothesis: true mean is not equal to 2.5
## 95 percent confidence interval:
## 3.394372 4.068591
## sample estimates:
## mean of x
## 3.731481
library(pwr)
library(effectsize)
##
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
##
## phi
model1<-t.test(lec2$ltnt_c1,mu=2.5)
print(model1)
##
## One Sample t-test
##
## data: lec2$ltnt_c1
## t = 7.2004, df = 215, p-value = 9.936e-12
## alternative hypothesis: true mean is not equal to 2.5
## 95 percent confidence interval:
## 3.394372 4.068591
## sample estimates:
## mean of x
## 3.731481
#report(model1)
cohens_d(model1) #Computing cohen's d for one sample t-test
## Cohen's d | 95% CI
## ------------------------
## 0.49 | [0.35, 0.63]
##
## - Deviation from a difference of 2.5.
FemaleLE <- (lec2$ltnt_c1[lec2$chsex_d1 == "Female"])
describe(FemaleLE)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 107 3.74 2.5 3 3.52 1.48 0 14 14 1.25 2.69 0.24
MaleLE <- (lec2$ltnt_c1[lec2$chsex_d1 == "Male"])
describe(MaleLE) #Computing M and SD of females and males negative life events
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 109 3.72 2.54 3 3.54 2.97 0 13 13 0.84 0.92 0.24
var.test(FemaleLE,MaleLE) #Looking at homogeneity of variance for adolescents' sex and negative life events
##
## F test to compare two variances
##
## data: FemaleLE and MaleLE
## F = 0.97044, num df = 106, denom df = 108, p-value = 0.8774
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.6632119 1.4209612
## sample estimates:
## ratio of variances
## 0.9704389
t.test(FemaleLE, MaleLE,var.equal=T) #Conducting a two sample t-test between adolescents' sex and negative life events
##
## Two Sample t-test
##
## data: FemaleLE and MaleLE
## t = 0.039511, df = 214, p-value = 0.9685
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.6622905 0.6893848
## sample estimates:
## mean of x mean of y
## 3.738318 3.724771
model2 <- t.test(FemaleLE, MaleLE,var.equal=T)
print(model2)
##
## Two Sample t-test
##
## data: FemaleLE and MaleLE
## t = 0.039511, df = 214, p-value = 0.9685
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.6622905 0.6893848
## sample estimates:
## mean of x mean of y
## 3.738318 3.724771
#report(model2)
cohens_d(model2) #Computing cohen's d for two sample t-test
## Warning: Missing values detected. NAs dropped.
## Cohen's d | 95% CI
## -------------------------
## 5.38e-03 | [-0.26, 0.27]
##
## - Estimated using pooled SD.
df2 <- subset(lec,select=c(ltnt_c1, ltpt_c1))
var.test(df2$ltnt_c1, df2$ltpt_c1) #Looking at homogeneity of variance for positive and negative life events
##
## F test to compare two variances
##
## data: df2$ltnt_c1 and df2$ltpt_c1
## F = 1.2493, num df = 215, denom df = 215, p-value = 0.1034
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.9555727 1.6333771
## sample estimates:
## ratio of variances
## 1.249324
t.test(df2$ltnt_c1, df2$ltpt_c1,var.equal= T,paired= T) #Conducting a paired samples t-test between adolescents' positive and negative life events
##
## Paired t-test
##
## data: df2$ltnt_c1 and df2$ltpt_c1
## t = -2.091, df = 215, p-value = 0.0377
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -0.83641140 -0.02469971
## sample estimates:
## mean difference
## -0.4305556
model3 <- t.test(df2$ltnt_c1, df2$ltpt_c1,var.equal= T,paired= T)
print(model3)
##
## Paired t-test
##
## data: df2$ltnt_c1 and df2$ltpt_c1
## t = -2.091, df = 215, p-value = 0.0377
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -0.83641140 -0.02469971
## sample estimates:
## mean difference
## -0.4305556
#report(model3)
cohens_d(model3) #Computing cohen's d for paired sample t-test
## Warning: Missing values detected. NAs dropped.
## Cohen's d | 95% CI
## --------------------------
## -0.14 | [-0.28, -0.01]
describe(df2$ltnt_c1)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 216 3.73 2.51 3 3.52 1.48 0 14 14 1.04 1.82 0.17
describe(df2$ltpt_c1) #Computing M and SD of positive and negative life events
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 216 4.16 2.25 4 4.05 2.97 0 11 11 0.55 0.14 0.15
dfSumm <- lec2 %>%
group_by(chsex_d1) %>%
summarise(
n=n(),
sd = sd(ltnt_c1, na.rm=T),
ltnt_c1 = mean(ltnt_c1,na.rm=T)
)%>%
mutate( se=sd/sqrt(n)) %>%
mutate( ci=se * 1.96)
ggplot(dfSumm, aes(chsex_d1,ltnt_c1)) +
geom_col(fill=c("blue", "red")) +
geom_errorbar(aes(ymin = ltnt_c1-ci, ymax = ltnt_c1+ci),width=0.3)+
xlab("Adolescent Sex")+
ylab("Negative Life Events") #Creating bar graph of t-test for two sample t-test

library(pwr)
library(effectsize)
pwr.t.test(d = .50, sig.level = .05, power = .8, type = "paired",
alternative = "two.sided") #Estimating power for t-test
##
## Paired t test power calculation
##
## n = 33.36713
## d = 0.5
## sig.level = 0.05
## power = 0.8
## alternative = two.sided
##
## NOTE: n is number of *pairs*
pwr.r.test(r = .30, sig.level = .05, power = .8) #Estimating power for correlation
##
## approximate correlation power calculation (arctangh transformation)
##
## n = 84.07364
## r = 0.3
## sig.level = 0.05
## power = 0.8
## alternative = two.sided
sessionInfo()
## R version 4.2.1 (2022-06-23)
## 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 pwr_1.3-0 dplyr_1.0.10 Rmisc_1.5.1
## [5] plyr_1.8.7 lattice_0.20-45 psych_2.2.5 tidyr_1.2.1
## [9] ggeffects_1.1.4 ggplot2_3.3.6 naniar_0.6.1 haven_2.5.1
##
## 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 rstudioapi_0.14 jquerylib_0.1.4
## [13] Matrix_1.4-1 rmarkdown_2.16 labeling_0.4.2 splines_4.2.1
## [17] stringr_1.4.1 munsell_0.5.0 compiler_4.2.1 xfun_0.33
## [21] parameters_0.19.0 pkgconfig_2.0.3 mnormt_2.1.0 mgcv_1.8-40
## [25] htmltools_0.5.3 insight_0.18.6 tidyselect_1.1.2 tibble_3.1.8
## [29] fansi_1.0.3 withr_2.5.0 grid_4.2.1 nlme_3.1-157
## [33] jsonlite_1.8.0 gtable_0.3.1 lifecycle_1.0.2 DBI_1.1.3
## [37] magrittr_2.0.3 bayestestR_0.13.0 scales_1.2.1 datawizard_0.6.3
## [41] cli_3.4.0 stringi_1.7.8 cachem_1.0.6 farver_2.1.1
## [45] bslib_0.4.0 ellipsis_0.3.2 generics_0.1.3 vctrs_0.4.1
## [49] tools_4.2.1 forcats_0.5.2 glue_1.6.2 purrr_0.3.4
## [53] hms_1.1.2 parallel_4.2.1 fastmap_1.1.0 yaml_2.3.5
## [57] colorspace_2.0-3 knitr_1.40 sass_0.4.2
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("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("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("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("plyr")
##
## To cite plyr in publications use:
##
## Hadley Wickham (2011). The Split-Apply-Combine Strategy for Data
## Analysis. Journal of Statistical Software, 40(1), 1-29. URL
## https://www.jstatsoft.org/v40/i01/.
##
## A BibTeX entry for LaTeX users is
##
## @Article{,
## title = {The Split-Apply-Combine Strategy for Data Analysis},
## author = {Hadley Wickham},
## journal = {Journal of Statistical Software},
## year = {2011},
## volume = {40},
## number = {1},
## pages = {1--29},
## url = {https://www.jstatsoft.org/v40/i01/},
## }
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("lattice")
##
## To cite the lattice package in publications use:
##
## Sarkar, Deepayan (2008) Lattice: Multivariate Data Visualization with
## R. Springer, New York. ISBN 978-0-387-75968-5
##
## A BibTeX entry for LaTeX users is
##
## @Book{,
## title = {Lattice: Multivariate Data Visualization with R},
## author = {Deepayan Sarkar},
## publisher = {Springer},
## address = {New York},
## year = {2008},
## note = {ISBN 978-0-387-75968-5},
## url = {http://lmdvr.r-forge.r-project.org},
## }
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 ("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("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("effectsize")
##
## To cite effectsize in publications use:
##
## Ben-Shachar M, Lüdecke D, Makowski D (2020). effectsize: Estimation
## of Effect Size Indices and Standardized Parameters. Journal of Open
## Source Software, 5(56), 2815. doi: 10.21105/joss.02815
##
## A BibTeX entry for LaTeX users is
##
## @Article{,
## title = {{e}ffectsize: Estimation of Effect Size Indices and Standardized Parameters},
## author = {Mattan S. Ben-Shachar and Daniel Lüdecke and Dominique Makowski},
## year = {2020},
## journal = {Journal of Open Source Software},
## volume = {5},
## number = {56},
## pages = {2815},
## publisher = {The Open Journal},
## doi = {10.21105/joss.02815},
## url = {https://doi.org/10.21105/joss.02815},
## }
citation("pwr")
##
## To cite package 'pwr' in publications use:
##
## Champely S (2020). _pwr: Basic Functions for Power Analysis_. R
## package version 1.3-0, <https://CRAN.R-project.org/package=pwr>.
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {pwr: Basic Functions for Power Analysis},
## author = {Stephane Champely},
## year = {2020},
## note = {R package version 1.3-0},
## url = {https://CRAN.R-project.org/package=pwr},
## }