Basic Statistics

Load Libraries

# 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 aggregate all non-grouping columns: take_all(mtcars, mean, by = am)

##Import Data

# Import the "fakedata.csv" file

d2 <- read.csv("projectdata.csv")


# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab

Univariate Plots: Histograms & Tables

Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.

# use tables to visualize categorical data
table(d2$gender)
## 
##             female I use another term               male  Prefer not to say 
##                509                 19                112                  9
table(d2$pet)
## 
##                  bird                   cat           cat and dog 
##                     3                   121                    77 
##                   dog                  fish multiple types of pet 
##                   102                    19                    58 
##               no pets                 other 
##                   231                    38
# use histograms to visualize continuous data
hist(d2$gad)

hist(d2$phq)

hist(d2$pss)

hist(d2$swemws)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##         vars   n    mean      sd  median trimmed     mad min  max range  skew
## X          1 649 5110.81 2587.67 5627.00 5241.03 3068.98  20 8860  8840 -0.36
## gender*    2 649    1.42    0.82    1.00    1.26    0.00   1    4     3  1.59
## pet*       3 649    5.00    2.10    6.00    5.06    2.97   1    8     7 -0.21
## pss        4 649    3.13    0.96    3.25    3.15    1.11   1    5     4 -0.11
## phq        5 649    2.28    0.86    2.22    2.23    0.99   1    4     3  0.37
## gad        6 649    2.19    0.94    2.00    2.13    1.06   1    4     3  0.43
## swemws     7 649    3.01    0.86    3.00    3.01    0.85   1    5     4  0.06
##         kurtosis     se
## X          -1.16 101.58
## gender*     0.83   0.03
## pet*       -1.53   0.08
## pss        -0.80   0.04
## phq        -0.92   0.03
## gad        -1.07   0.04
## swemws     -0.43   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).

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, gender, pet)
 pet 
 bird   cat   cat and dog   dog   fish   multiple types of pet   no pets   other 
 gender 
   I use another term  3 2 5 3 5 1
   Prefer not to say  2 1 2 1 3
   female  3 92 70 73 15 42 186 28
   male  24 5 23 2 12 37 9
   #Total cases  3 121 77 102 19 58 231 38
# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$gad, d2$phq, 
     main="Scatterplot of Anxiety and Depression",
     xlab = "Anxiety",
     ylab = "Depression")

plot(d2$pss, d2$swemws,
     main="Scatterplot of Percieved Stress and Mental Well-Being",
     xlab = "Percieved Stress",
     ylab = "Mental Well-Being")

# 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 ~ gender,
        main = "Boxplot of Gender and Anxiety",
        xlab = "Gender",
        ylab = "Anxiety")

boxplot(data = d2, phq ~ pet,
        main = "Boxplot of Pet Ownership and Depression",
        xlab = "Pet Ownership",
        ylab = "Depression")

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