# 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 all non-grouping columns: take_all(mtcars, mean, by = am)
# Import the "fakedata_2025.csv" file
d2 <- read.csv("Data/projectdata.csv")
str(d2)
## 'data.frame': 251 obs. of 7 variables:
## $ X : int 7888 7365 8747 7357 8760 8654 8272 8738 7911 8463 ...
## $ relationship_status: chr "Single, never married" "Single, never married" "Single, never married" "Single, never married" ...
## $ exercise : chr "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" "2 1-2 hours" ...
## $ big5_con : num 4 3.33 6.33 5 6.67 ...
## $ rse : num 2.4 1.4 4 3.4 3.9 3.6 3.2 3.3 3 2.2 ...
## $ pas_covid : num 4.44 3.11 3.22 1.78 2.78 ...
## $ brs : num 2 3.83 3.83 4 4.67 ...
# 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$relationship_status)
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 33
## Prefer not to say
## 21
## Single, divorced or widowed
## 1
## Single, never married
## 195
table(d2$exercise)
##
## 1 less than 1 hour 2 1-2 hours 3 2-5 hours 4 5-8 hours
## 53 98 72 19
## 5 over 8 hours
## 9
# use histograms to visualize continuous data (4 variables)
hist(d2$rse)
hist(d2$pas_covid)
hist(d2$brs)
hist(d2$big5_con)
describe(d2)
## vars n mean sd median trimmed mad min
## X 1 251 7557.87 757.48 7531.00 7556.19 987.41 6291.00
## relationship_status* 2 251 4.42 1.12 5.00 4.65 0.00 1.00
## exercise* 3 251 2.33 1.01 2.00 2.25 1.48 1.00
## big5_con 4 251 4.39 1.20 4.33 4.38 0.99 1.67
## rse 5 251 2.23 0.67 2.10 2.21 0.74 1.00
## pas_covid 6 251 3.34 0.63 3.33 3.36 0.49 1.22
## brs 7 251 2.69 0.86 2.67 2.68 0.99 1.00
## max range skew kurtosis se
## X 8860 2569.00 0.02 -1.27 47.81
## relationship_status* 5 4.00 -1.50 0.49 0.07
## exercise* 5 4.00 0.58 0.03 0.06
## big5_con 7 5.33 0.03 -0.42 0.08
## rse 4 3.00 0.35 -0.42 0.04
## pas_covid 5 3.78 -0.25 0.55 0.04
## brs 5 4.00 0.12 -0.61 0.05
## 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.
Crosstabs are used to visualize combinations of two categorical variables.
cross_cases(d2, relationship_status, exercise)
|  exercise | |||||
|---|---|---|---|---|---|
|  1 less than 1 hour |  2 1-2 hours |  3 2-5 hours |  4 5-8 hours |  5 over 8 hours | |
|  relationship_status | |||||
|    In a relationship/married and cohabiting | 1 | ||||
|    In a relationship/married but living apart | 7 | 11 | 9 | 4 | 2 |
|    Prefer not to say | 5 | 11 | 4 | 1 | |
|    Single, divorced or widowed | 1 | ||||
|    Single, never married | 40 | 76 | 58 | 14 | 7 |
|    #Total cases | 53 | 98 | 72 | 19 | 9 |
## 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$pas_covid,
main="Scatterplot of Rosenberg Self-Esteem Inventory and Pandemic Anxiety Scale",
xlab = "Rosenberg Self-Esteem Inventory",
ylab = "Pandemic ANxiety Scale")
plot(d2$rse, d2$brs,
main="Scatterplot of Rosenberg Self-Esteem Inventory and Brief Resilience Scale",
xlab = "Rosenberg Self-Esteem Inventory",
ylab = "Brief Resilience 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 are used to visualize combinations of one categorical and one continuous variable.
# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable'
boxplot(data=d2, rse~relationship_status,
main="Boxplot of Rosenberg Self-Esteem Inventory by Relationship Status",
xlab = "Relationships Status",
ylab = "Rosenberg Self-Esteem Inventory")
boxplot(data=d2, brs~exercise,
main="Boxplot of Brief Resilience Scale by Exercise ",
xlab = "Exercise",
ylab = "Brief Resilience Scale")
# 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!!