# 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("expss")
library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To select columns from data: columns(mtcars, mpg, vs:carb)
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## To return to the console output, use 'expss_output_default()'.
# Import the "fakedata_2025.csv" file
d2 <- read.csv("Data/projectdata.csv")
str(d2)
## 'data.frame': 1280 obs. of 7 variables:
## $ X : int 1 321 401 469 520 1390 1422 1849 2183 2247 ...
## $ sleep_hours: chr "3 7-8 hours" "4 8-10 hours" "2 5-6 hours" "3 7-8 hours" ...
## $ mhealth : chr "none or NA" "none or NA" "obsessive compulsive disorder" "depression" ...
## $ covid_neg : int 0 0 0 0 0 0 0 0 0 0 ...
## $ gad : num 1.86 1 2.14 1.71 1.14 ...
## $ pss : num 3.25 2.25 2.25 2.25 2.75 2.75 4.75 3.25 3.5 2.25 ...
## $ support : num 2.5 2.5 3.83 4.67 2.83 ...
# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab
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$sleep_hours)
##
## 1 < 5 hours 2 5-6 hours 3 7-8 hours 4 8-10 hours 5 > 10 hours
## 88 347 499 292 54
table(d2$mhealth)
##
## anxiety disorder bipolar
## 129 7
## depression eating disorders
## 33 31
## none or NA obsessive compulsive disorder
## 989 27
## other ptsd
## 37 27
# use histograms to visualize continuous data (4 variables)
hist(d2$covid_neg)
hist(d2$gad)
hist(d2$pss)
hist(d2$support)
describe(d2)
## vars n mean sd median trimmed mad min max range
## X 1 1280 4701.89 2602.47 4817.00 4756.43 3392.19 1 8867 8866
## sleep_hours* 2 1280 2.90 0.97 3.00 2.91 1.48 1 5 4
## mhealth* 3 1280 4.65 1.41 5.00 4.88 0.00 1 8 7
## covid_neg 4 1280 1.08 1.77 0.00 0.72 0.00 0 8 8
## gad 5 1280 2.04 0.91 1.71 1.95 0.85 1 4 3
## pss 6 1280 2.93 0.95 3.00 2.92 1.11 1 5 4
## support 7 1280 3.57 0.95 3.67 3.62 0.99 1 5 4
## skew kurtosis se
## X -0.13 -1.24 72.74
## sleep_hours* 0.02 -0.45 0.03
## mhealth* -1.37 2.52 0.04
## covid_neg 1.44 0.91 0.05
## gad 0.68 -0.72 0.03
## pss 0.09 -0.75 0.03
## support -0.43 -0.54 0.03
## 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.
We analyzed the skew and kurtosis of our continuous variables and 5 were within the accepted range (-2/+2). However, 1 variable, mental health, was outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.
Crosstabs are used to visualize combinations of two categorical variables.
cross_cases(d2, sleep_hours, mhealth)
|  mhealth | ||||||||
|---|---|---|---|---|---|---|---|---|
|  anxiety disorder |  bipolar |  depression |  eating disorders |  none or NA |  obsessive compulsive disorder |  other |  ptsd | |
|  sleep_hours | ||||||||
|    1 < 5 hours | 20 | 3 | 1 | 3 | 46 | 2 | 6 | 7 |
|    2 5-6 hours | 43 | 1 | 12 | 10 | 250 | 8 | 10 | 13 |
|    3 7-8 hours | 40 | 12 | 11 | 405 | 12 | 15 | 4 | |
|    4 8-10 hours | 20 | 3 | 6 | 6 | 247 | 2 | 5 | 3 |
|    5 > 10 hours | 6 | 2 | 1 | 41 | 3 | 1 | ||
|    #Total cases | 129 | 7 | 33 | 31 | 989 | 27 | 37 | 27 |
## 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(~grade+family_type,data=d2)
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$covid_neg, d2$gad,
main="Scatterplot of Negative Effects of Covid and General Anxiety disorder",
xlab = "Negative Effects of Covid",
ylab = "General Anxiety Disorder")
plot(d2$pss, d2$support,
main="Scatterplot of Perceived Stress and Social Support",
xlab = "Percieved Stress",
ylab = "Social Support")
# 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 are used to visualize combinations of one categorical and one continuous variable.
# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable'
boxplot(data=d2, covid_neg~sleep_hours,
main="Boxplot of Negative Effects of Covid by Hours of Sleep",
xlab = "Hours of Sleep",
ylab = "Negative effects of Covid")
boxplot(data=d2, support~mhealth,
main="Boxplot of Social Support by Mental Health",
xlab = "Mental Health",
ylab = "Social Support")
# 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!!