# if you haven't used a given package before, you'll need to download it first
# delete the "#" before the install function and run it to download
# re-insert the "#" 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 select rows from data: rows(mtcars, am==0)
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## To return to the console output, use 'expss_output_default()'.
##Import Data
# Import the "projectdata.csv" file
d2 <- read.csv("Data/projectdata.csv")
# 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
table(d2$age)
##
## 1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45
## 607 55 6 86
## 5 over 45
## 177
table(d2$mhealth)
##
## anxiety disorder bipolar
## 95 3
## depression eating disorders
## 24 18
## none or NA obsessive compulsive disorder
## 739 17
## other ptsd
## 23 12
# use histograms to visualize continuous data
hist(d2$big5_open)
hist(d2$iou)
hist(d2$mfq_state)
hist(d2$pas_covid)
describe(d2)
## vars n mean sd median trimmed mad min max range
## X 1 931 4703.08 2584.27 4833.00 4756.44 3319.54 20 8860.00 8840.00
## age* 2 931 2.11 1.65 1.00 1.89 0.00 1 5.00 4.00
## mhealth* 3 931 4.62 1.37 5.00 4.89 0.00 1 8.00 7.00
## big5_open 4 931 5.24 1.11 5.33 5.33 0.99 1 7.00 6.00
## iou 5 931 2.53 0.89 2.41 2.47 0.99 1 4.78 3.78
## mfq_state 6 931 4.14 0.96 4.25 4.19 0.93 1 6.00 5.00
## pas_covid 7 931 3.22 0.68 3.22 3.24 0.66 1 5.00 4.00
## skew kurtosis se
## X -0.13 -1.22 84.70
## age* 0.96 -0.93 0.05
## mhealth* -1.59 2.81 0.04
## big5_open -0.76 0.54 0.04
## iou 0.49 -0.63 0.03
## mfq_state -0.50 0.12 0.03
## pas_covid -0.24 0.09 0.02
## 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, age, mhealth)
 mhealth | ||||||||
---|---|---|---|---|---|---|---|---|
 anxiety disorder |  bipolar |  depression |  eating disorders |  none or NA |  obsessive compulsive disorder |  other |  ptsd | |
 age | ||||||||
   1 under 18 | 64 | 2 | 11 | 15 | 477 | 12 | 17 | 9 |
   2 between 18 and 25 | 10 | 1 | 1 | 3 | 36 | 3 | 1 | |
   3 between 26 and 35 | 1 | 5 | ||||||
   4 between 36 and 45 | 12 | 4 | 65 | 2 | 3 | |||
   5 over 45 | 8 | 8 | 156 | 2 | 3 | |||
   #Total cases | 95 | 3 | 24 | 18 | 739 | 17 | 23 | 12 |
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$mfq_state, d2$pas_covid,
main="Scatterplot of Mental Flexibility and Pandemic Anxiety",
xlab = "Mental Flexibility",
ylab = "Pandemic Anxiety")
plot(d2$big5_open, d2$iou,
main="Scatterplot of Openness and Intolerance of Uncertainty",
xlab = "Openness",
ylab = "Intolerance of Uncertainty")
# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your 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 variable names, not their 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, big5_open~age,
main="Boxplot of Age and Openness",
xlab = "Age",
ylab = "Openness")
boxplot(data=d2, iou~mhealth,
main="Boxplot of Mental Health Disorders and Intolerance of Uncertainty",
xlab = "Mental Health Disorders",
ylab = "Intolerance of Uncertainty")
# 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 hypotheses. You may repeat 1 variable to see its association with others. Again, 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 variable names, not their scales, so someone reading your plots can understand them.
We did it!!