# 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
##
## Use magrittr pipe '%>%' to chain several operations:
## mtcars %>%
## let(mpg_hp = mpg/hp) %>%
## take(mean(mpg_hp), by = am)
##
##
## 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$relationship_status)
##
## In a relationship/married and cohabiting
## 1
## In a relationship/married but living apart
## 68
## Prefer not to say
## 72
## Single, divorced or widowed
## 2
## Single, never married
## 541
table(d2$mhealth)
##
## anxiety disorder bipolar
## 80 3
## depression eating disorders
## 10 18
## none or NA obsessive compulsive disorder
## 528 16
## other ptsd
## 17 12
# use histograms to visualize continuous data
hist(d2$pswq)
hist(d2$mfq_26)
hist(d2$rse)
hist(d2$phq)
describe(d2)
## vars n mean sd median trimmed mad min
## X 1 684 5171.60 2568.03 5768.00 5316.48 2919.98 20.0
## relationship_status* 2 684 4.48 1.04 5.00 4.73 0.00 1.0
## mhealth* 3 684 4.59 1.45 5.00 4.83 0.00 1.0
## pswq 4 684 2.66 0.75 2.71 2.67 0.85 1.0
## mfq_26 5 684 4.24 0.64 4.30 4.25 0.59 1.8
## rse 6 684 2.51 0.71 2.50 2.50 0.74 1.0
## phq 7 684 2.27 0.85 2.22 2.22 0.99 1.0
## max range skew kurtosis se
## X 8860.0 8840 -0.42 -1.10 98.19
## relationship_status* 5.0 4 -1.63 0.94 0.04
## mhealth* 8.0 7 -1.43 2.17 0.06
## pswq 4.0 3 -0.15 -0.98 0.03
## mfq_26 5.8 4 -0.28 0.12 0.02
## rse 4.0 3 0.05 -0.76 0.03
## phq 4.0 3 0.37 -0.88 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 all were within the accepted range (-2/+2).
Crosstabs are used to visualize combinations of two categorical variables.
cross_cases(d2, relationship_status, mhealth)
|  mhealth | ||||||||
|---|---|---|---|---|---|---|---|---|
|  anxiety disorder |  bipolar |  depression |  eating disorders |  none or NA |  obsessive compulsive disorder |  other |  ptsd | |
|  relationship_status | ||||||||
|    In a relationship/married and cohabiting | 1 | |||||||
|    In a relationship/married but living apart | 13 | 1 | 2 | 44 | 4 | 1 | 3 | |
|    Prefer not to say | 10 | 1 | 2 | 53 | 2 | 4 | ||
|    Single, divorced or widowed | 1 | 1 | ||||||
|    Single, never married | 57 | 2 | 9 | 14 | 429 | 9 | 12 | 9 |
|    #Total cases | 80 | 3 | 10 | 18 | 528 | 16 | 17 | 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$pswq, d2$rse,
main="Scatterplot of Penn State Worry Questionnaire and Rosenberg Self-Esteem Inventory",
xlab = "Penn State Worry Questionnaire",
ylab = "Rosenberg Self-Esteem Inventory")
plot(d2$mfq_26, d2$phq,
main="Scatterplot of Mental Flexibility Questionnaire and Patient Health Questionnaire-9 ",
xlab = "Mental Flexibility Questionnaire",
ylab = "Patient Health Questionnaire-9")
# 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, rse ~ relationship_status,
main="Boxplot of Relationship Status and Rosenberg Self-Esteem Inventory",
xlab = "Relationship Status",
ylab = "Rosenberg Self-Esteem Inventory")
boxplot(data=d2, mfq_26 ~ mhealth,
main="Boxplot of Mental Health Disorders and Mental Flexibility Questionnaire",
xlab = "Mental Health Disorders",
ylab = "Mental Flexibility Questionnaire")
# 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!!