Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# after download is finished, insert a "#" before the install function so that the file will Knit later
# then run the library function calling that package

#install.packages("psych")
#install.packages("expss")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To aggregate data: take(mtcars, mean_mpg = mean(mpg), by = am)
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

Import & Examine Data

# Import the "fakedata_2025.csv" file

d2 <- read.csv("Data/projectdata.csv")

str(d2)
## 'data.frame':    290 obs. of  7 variables:
##  $ X        : int  7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
##  $ education: chr  "2 equivalent to high school completion" "1 equivalent to not completing high school" "2 equivalent to high school completion" "2 equivalent to high school completion" ...
##  $ mhealth  : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ pswq     : num  3.43 2.14 1.57 1.43 1.5 ...
##  $ pas_covid: num  4.44 3.11 3.22 1.78 2.78 ...
##  $ brs      : num  2 3.83 3.83 4 4.67 ...
##  $ support  : num  4.83 5 4.83 3.33 4.83 ...
# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab

Univariate Plots: Histograms & Tables

Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.

# use tables to visualize categorical data (2 variables)
table(d2$education)
## 
##              1 equivalent to not completing high school 
##                                                      70 
##                  2 equivalent to high school completion 
##                                                     143 
## 3 equivalent to vocational/technical program completion 
##                                                       3 
##                        4 equivalent to AP/IB completion 
##                                                      52 
##                                       prefer not to say 
##                                                      22
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            39                             3 
##                    depression              eating disorders 
##                             5                            14 
##                    none or NA obsessive compulsive disorder 
##                           198                            11 
##                         other                          ptsd 
##                            13                             7
# use histograms to visualize continuous data (4 variables)
hist(d2$pswq)

hist(d2$pas_covid)

hist(d2$brs)

hist(d2$support)

Univariate Normality for Continuous Variables

describe(d2)
##            vars   n    mean     sd  median trimmed    mad     min  max   range
## X             1 290 7549.38 742.10 7490.00 7543.68 946.64 6291.00 8860 2569.00
## education*    2 290    2.36   1.24    2.00    2.22   1.48    1.00    5    4.00
## mhealth*      3 290    4.55   1.61    5.00    4.69   0.00    1.00    8    7.00
## pswq          4 290    2.91   0.68    3.00    2.95   0.74    1.14    4    2.86
## pas_covid     5 290    3.35   0.64    3.44    3.37   0.66    1.00    5    4.00
## brs           6 290    2.66   0.87    2.67    2.66   0.99    1.00    5    4.00
## support       7 290    3.25   0.98    3.25    3.25   1.11    1.00    5    4.00
##             skew kurtosis    se
## X           0.07    -1.21 43.58
## education*  0.83    -0.50  0.07
## mhealth*   -1.05     1.00  0.09
## pswq       -0.43    -0.62  0.04
## pas_covid  -0.40     0.71  0.04
## brs         0.10    -0.64  0.05
## support    -0.07    -0.81  0.06
## For the required write-up below, choose one of these options to paste and edit below based on your output.

## OPTION 1
# We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).

## OPTION 2
# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name(s) here) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

Write-up of Normality

We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, education, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 education 
   1 equivalent to not completing high school  7 1 5 51 2 3 1
   2 equivalent to high school completion  14 3 6 106 4 5 5
   3 equivalent to vocational/technical program completion  1 1 1
   4 equivalent to AP/IB completion  12 1 2 30 4 3
   prefer not to say  5 1 2 1 10 1 2
   #Total cases  39 3 5 14 198 11 13 7
## Some students may have issues with this function working. If this happens to you, please try these 2 options:
## Option 1: install the "maditr" package and then call in its library.
## Option 2: If Option 1 doesn't work, then you will use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line.

# xtabs(~ + , data=)

# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$pas_covid, d2$pswq,
     main="Scatterplot of Pandemic Anxiety and Overall Worry",
     xlab = "Pandemic Anxiety",
     ylab = "Overall Worry")

plot(d2$pas_covid, d2$brs,
     main="Scatterplot of Pandemic Anxiety and Resilience",
     xlab = "Pandemic Anxiety",
     ylab = "Resilience")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your potential hypotheses. You may repeat 1 variable to see its association with 2 others. You will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.

Boxplots

Boxplots are used to visualize combinations of one categorical and one continuous variable.

# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable' 

boxplot(data=d2, pas_covid~mhealth,
        main="Boxplot of Pandemic Anxiety by Mental Health Diagnosis",
        xlab = "Mental Health Diagnosis",
        ylab = "Pandemic Anxiety")

boxplot(data=d2, pas_covid~education,
        main="Boxplot of Pandemic Anxiety by Education Level",
        xlab = "Education Level",
        ylab = "Pandemic Anxiety")

# Note: for HW, you will choose to plot 2 combos of any of your 4 continuous variables with either of your 2 categorical variables, based on your potential hypotheses. You may repeat 1 variable to see its association with others. Again, will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.

That’s it!!