# if you haven't used a given package before, you'll need to download it first
# insert the "#" before the install function so that the file will Knit later
# then run the library function calling that package
#install.packages("")
library(psych) # for the describe() command
# Import the "fakedata.csv" file for this Lab
d2 <- read.csv("Data/projectdata.csv")
# Note: for your Project version, you will import "projectdata.csv" that you created and exported at the end of the Project Data Prep assignment.
Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.
# use tables to visualize 2 categorical variables
table(d2$age)
##
## 1 under 18 2 between 18 and 25
## 614 52
table(d2$mhealth)
##
## anxiety disorder bipolar
## 76 3
## depression eating disorders
## 11 19
## none or NA obsessive compulsive disorder
## 515 15
## other ptsd
## 18 9
# use histograms to visualize 4 continuous variables
hist(d2$iou)
hist(d2$mfq_26)
hist(d2$mfq_state)
hist(d2$pss)
# quick & dirty normality test: check skew & kurtosis values
describe(d2)
## vars n mean sd median trimmed mad min max range
## X 1 666 5165.34 2603.37 5768.00 5310.28 3063.05 20.0 8860.00 8840.00
## age* 2 666 1.08 0.27 1.00 1.00 0.00 1.0 2.00 1.00
## mhealth* 3 666 4.59 1.43 5.00 4.83 0.00 1.0 8.00 7.00
## iou 4 666 2.68 0.92 2.56 2.65 1.04 1.0 4.78 3.78
## mfq_26 5 666 4.25 0.65 4.30 4.26 0.59 1.8 6.00 4.20
## mfq_state 6 666 3.98 0.98 4.00 4.02 0.93 1.0 6.00 5.00
## pss 7 666 3.13 0.95 3.25 3.15 1.11 1.0 5.00 4.00
## skew kurtosis se
## X -0.40 -1.13 100.88
## age* 3.14 7.86 0.01
## mhealth* -1.50 2.27 0.06
## iou 0.29 -0.85 0.04
## mfq_26 -0.26 0.13 0.03
## mfq_state -0.35 -0.02 0.04
## pss -0.12 -0.76 0.04
## 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 four continuous variables and all were within the acceptable range (-2/+2).
## OPTION 2
# We analyzed the skew and kurtosis of our four continuous variables and (#) were within the acceptable range (-2/+2). However, (#) variables (list variable name(s) here) were outside of the acceptable range. For our analysis we will use them anyway, but outside of class this is bad practice.
# Objective normality test: Shapiro-Wilks test
# We want the test to be NON-SIG (aka > .05) to indicate normality
options(scipen = 999)
# the above code turns off scientific notation
shapiro.test(d2$iou)
##
## Shapiro-Wilk normality test
##
## data: d2$iou
## W = 0.9705, p-value = 0.0000000002528
shapiro.test(d2$mfq_26)
##
## Shapiro-Wilk normality test
##
## data: d2$mfq_26
## W = 0.99481, p-value = 0.02339
shapiro.test(d2$mfq_state)
##
## Shapiro-Wilk normality test
##
## data: d2$mfq_state
## W = 0.98599, p-value = 0.00000528
shapiro.test(d2$pss)
##
## Shapiro-Wilk normality test
##
## data: d2$pss
## W = 0.97976, p-value = 0.00000005783
We analyzed the skew and kurtosis of our four continuous variables and all were within the acceptable range (-2/+2).
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$iou, d2$mfq_26,
main="Scatterplot of Intolerance of Uncertaitny Scale and Mental Flexibility Questionnaire Trait",
xlab = "Intolerance of Uncertainty Scale",
ylab = "Mental Flexibility Questionnaire Trait")
plot(d2$mfq_state, d2$pss,
main="Scatterplot of Mental Flexibility Questionnaire State and Percieved Stress Scale",
xlab = "Mental Flexibility Questionnaire State",
ylab = "Percieved Stress Scale")
# 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, iou~mhealth,
main="Boxplot of Social Support by Grade ",
xlab = "Intolerance of Uncertainty",
ylab = "Mental health Disorders")
boxplot(data=d2, mfq_state~age,
main="Boxplot of Extroversion by Family Type ",
xlab = "Mental Flecibility Quiestionnaire State",
ylab = "age")
# 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.
Those are the basics!!