# 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)
# Import the "fakedata_2025.csv" file
d2 <- read.csv("Data/projectdata.csv")
str(d2)
## 'data.frame': 140 obs. of 7 variables:
## $ X : int 8120 8301 8308 8389 8536 8578 8660 8680 8738 8106 ...
## $ employment : chr "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" "1 high school equivalent" ...
## $ sleep_hours: chr "4 8-10 hours" "3 7-8 hours" "3 7-8 hours" "1 < 5 hours" ...
## $ rse : num 2.1 2.9 2.4 1.2 1.2 1.9 2.7 2.9 3.3 3.5 ...
## $ pss : num 3 4 3 5 3.5 2.75 3.5 2.5 2.5 3.75 ...
## $ mfq_26 : num 3.55 4.5 3.4 4.6 3.8 3.6 4.45 4.5 4.75 5.1 ...
## $ school_att : num 3.62 3.2 4 2.6 3.89 ...
# 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$employment)
##
## 1 high school equivalent 2 college/university 3 employed
## 135 2 2
## 4 unemployed
## 1
table(d2$sleep_hours)
##
## 1 < 5 hours 2 5-6 hours 3 7-8 hours 4 8-10 hours 5 > 10 hours
## 16 44 54 23 3
# use histograms to visualize continuous data (4 variables)
hist(d2$rse)
hist(d2$pss)
hist(d2$mfq_26)
hist(d2$school_att)
describe(d2)
## vars n mean sd median trimmed mad min max
## X 1 140 8356.51 304.78 8370.50 8358.27 398.82 7847.00 8860.00
## employment* 2 140 1.06 0.36 1.00 1.00 0.00 1.00 4.00
## sleep_hours* 3 140 2.66 0.96 3.00 2.68 1.48 1.00 5.00
## rse 4 140 2.35 0.65 2.35 2.33 0.67 1.00 4.00
## pss 5 140 3.29 0.94 3.50 3.29 1.11 1.00 5.00
## mfq_26 6 140 4.19 0.62 4.20 4.18 0.59 2.75 5.75
## school_att 7 140 3.95 1.05 4.00 3.93 1.20 1.40 6.33
## range skew kurtosis se
## X 1013.00 -0.05 -1.35 25.76
## employment* 3.00 6.13 39.25 0.03
## sleep_hours* 4.00 0.07 -0.49 0.08
## rse 3.00 0.24 -0.48 0.05
## pss 4.00 -0.10 -0.82 0.08
## mfq_26 3.00 0.07 -0.41 0.05
## school_att 4.93 0.12 -0.56 0.09
## 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 all were within the accepted range (-2/+2).
Crosstabs are used to visualize combinations of two categorical variables.
cross_cases(d2, employment, sleep_hours)
|  sleep_hours | |||||
|---|---|---|---|---|---|
|  1 < 5 hours |  2 5-6 hours |  3 7-8 hours |  4 8-10 hours |  5 > 10 hours | |
|  employment | |||||
|    1 high school equivalent | 15 | 43 | 52 | 22 | 3 |
|    2 college/university | 1 | 1 | |||
|    3 employed | 1 | 1 | |||
|    4 unemployed | 1 | ||||
|    #Total cases | 16 | 44 | 54 | 23 | 3 |
## 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 are used to visualize combinations of two continuous variables.
plot(d2$rse, d2$mfq_26,
main="Scatterplot of Pandemic Anxiety and Mental Flexibility",
xlab = "Pandemic Anxiety",
ylab = "Mental Flexibility")
plot(d2$rse, d2$pss,
main="Scatterplot of Self-Esteem and School Support",
xlab = "Self-Esteem",
ylab = "School 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, rse~employment,
main="Boxplot of Self Esteem by Employment",
xlab = "Employment",
ylab = "Self Esteem")
boxplot(data=d2, pss~sleep_hours,
main="Boxplot of Pandemic Anxiety by Hours of Sleep",
xlab = "Hours of Sleep",
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!!