Basic Statistics

Load Libraries

# 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 Data

# 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.

Univariate Plots: Histograms & Tables

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$gender)
## 
##             female I use another term               male  Prefer not to say 
##                528                 19                113                  9
table(d2$age)
## 
##          1 under 18 2 between 18 and 25 
##                 615                  54
# use histograms to visualize 4 continuous variables
hist(d2$iou)

hist(d2$pss)

hist(d2$gad)

hist(d2$edeq12)

Univariate Normality for Continuous Variables (individually)

# quick & dirty normality test: check skew & kurtosis values
describe(d2)
##         vars   n    mean      sd  median trimmed     mad min     max   range
## X          1 669 5181.42 2600.27 5794.00 5330.83 3003.75  20 8860.00 8840.00
## gender*    2 669    1.41    0.81    1.00    1.24    0.00   1    4.00    3.00
## age*       3 669    1.08    0.27    1.00    1.00    0.00   1    2.00    1.00
## iou        4 669    2.71    0.93    2.59    2.67    1.04   1    4.89    3.89
## pss        5 669    3.15    0.95    3.25    3.17    1.11   1    5.00    4.00
## gad        6 669    2.21    0.93    2.00    2.15    1.06   1    4.00    3.00
## edeq12     7 669    1.96    0.77    1.83    1.90    0.86   1    4.00    3.00
##          skew kurtosis     se
## X       -0.42    -1.12 100.53
## gender*  1.62     0.94   0.03
## age*     3.07     7.45   0.01
## iou      0.28    -0.87   0.04
## pss     -0.15    -0.76   0.04
## gad      0.41    -1.06   0.04
## edeq12   0.53    -0.84   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 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 (5) were within the acceptable range (-2/+2). However, (1) variables (age) 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.97064, p-value = 0.0000000002533
shapiro.test(d2$pss)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$pss
## W = 0.97892, p-value = 0.00000003147
shapiro.test(d2$gad)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$gad
## W = 0.92454, p-value < 0.00000000000000022
shapiro.test(d2$edeq12)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$edeq12
## W = 0.92702, p-value < 0.00000000000000022

We analyzed the skew and kurtosis of our four continuous variables and all were within the acceptable range (-2/+2).

Bivariate Plots

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$gad, d2$iou,
     main="Scatterplot of Generalized Anxiety and Intolerance of Uncertainty ",
     xlab = "Generalized Anxiety",
     ylab = "Intolerance of Uncertainty")

plot(d2$gad, d2$pss,
     main="Scatterplot of Generalized Anxiety and Perceived Stress",
     xlab = "Generalized Anxiety",
     ylab = "Percieved Stress")

# 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

Boxplots are used to visualize combinations of one categorical and one continuous variable.

# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable' 

boxplot(data=d2, gad~age,
        main="Boxplot of Generalized Anxiety by Age",
        xlab = "Age",
        ylab = "Generalized Anxiety")

boxplot(data=d2, iou~gender,
        main="Boxplot of Intolerance of Uncertainty by Gender ",
        xlab = "Gender",
        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.

Those are the basics!!