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 several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    581 obs. of  7 variables:
##  $ X        : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ mhealth  : chr  "none or NA" "none or NA" "none or NA" "none or NA" ...
##  $ exercise : chr  "1 less than 1 hour" "1 less than 1 hour" "1 less than 1 hour" "1 less than 1 hour" ...
##  $ big5_con : num  3 4 6 4 3.33 ...
##  $ pas_covid: num  3 3.44 4.67 2.44 1.56 ...
##  $ pss      : num  2.75 2.25 3 2 1.75 2 1 1.25 3 1.25 ...
##  $ swemws   : num  3 2.86 4 3.57 3.86 ...
# 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$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            64                             3 
##                    depression              eating disorders 
##                            10                            15 
##                    none or NA obsessive compulsive disorder 
##                           456                            10 
##                         other                          ptsd 
##                            14                             9
table(d2$exercise)
## 
## 1 less than 1 hour        2 1-2 hours        3 2-5 hours        4 5-8 hours 
##                118                269                144                 27 
##     5 over 8 hours 
##                 23
# use histograms to visualize continuous data (4 variables)
hist(d2$big5_con)

hist(d2$pas_covid)

hist(d2$pss)

hist(d2$swemws)

Univariate Normality for Continuous Variables

describe(d2)
##           vars   n    mean      sd  median trimmed     mad   min  max   range
## X            1 581 5052.12 2611.63 5592.00 5179.21 3156.46 20.00 8860 8840.00
## mhealth*     2 581    4.60    1.41    5.00    4.85    0.00  1.00    8    7.00
## exercise*    3 581    2.26    0.96    2.00    2.16    1.48  1.00    5    4.00
## big5_con     4 581    4.50    1.16    4.33    4.52    0.99  1.00    7    6.00
## pas_covid    5 581    3.21    0.66    3.22    3.23    0.66  1.22    5    3.78
## pss          6 581    3.11    0.95    3.25    3.12    1.11  1.00    5    4.00
## swemws       7 581    3.02    0.87    3.00    3.02    0.85  1.00    5    4.00
##            skew kurtosis     se
## X         -0.34    -1.17 108.35
## mhealth*  -1.50     2.45   0.06
## exercise*  0.84     0.77   0.04
## big5_con  -0.14    -0.15   0.05
## pas_covid -0.25     0.17   0.03
## pss       -0.14    -0.76   0.04
## swemws     0.02    -0.41   0.04
## 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, mhealth, exercise)
 exercise 
 1 less than 1 hour   2 1-2 hours   3 2-5 hours   4 5-8 hours   5 over 8 hours 
 mhealth 
   anxiety disorder  13 29 16 5 1
   bipolar  1 2
   depression  1 5 2 2
   eating disorders  4 4 5 1 1
   none or NA  93 214 112 19 18
   obsessive compulsive disorder  5 3 2
   other  1 8 4 1
   ptsd  6 3
   #Total cases  118 269 144 27 23
## 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$big5_con, d2$swemws,
     main="Scatterplot of Conscientiousness and Short Warwick-Edinburgh Mental Wellbeing Scale",
     xlab = "Conscientiousness",
     ylab = "Short Warwick-Edinburgh Mental Wellbeing Scale")

plot(d2$pas_covid, d2$pss,
     main="Scatterplot of Pandemic Anxeity Scale and Percieved Stress Scale",
     xlab = "Pandemic Anxeity Scale",
     ylab = "Percieved Stress Scale")

# 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, swemws~mhealth, 
        main="Boxplot of Mental Health by  Short Warwick-Edinburgh Mental Wellbeing Scale",
        xlab = "Short Warwick-Edinburgh Mental Wellbeing Scale",
        ylab = "Mental Health")

boxplot(data=d2, pss~exercise,
        main="Boxplot of Exercise by Percieved Stress Scale ",
        xlab = "Percieved Stress Scale",
        ylab = "Exercise")

# 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!!