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
## 
## Use magrittr pipe '%>%' to chain several operations:
##              mtcars %>%
##                  let(mpg_hp = mpg/hp) %>%
##                  take(mean(mpg_hp), by = am)
## 

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

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 
##                950                 29                168                 18
table(d2$health)
## 
##                           cancer                         diabetes 
##                                1                                4 
##                    heart disease              high blood pressure 
##                                2                               22 
##                     lung disease   other chronic health condition 
##                               57                               28 
##                 other disability                prefer not to say 
##                               27                               24 
## something else or not applicable         three or more conditions 
##                              950                                9 
##                   two conditions 
##                               41
# use histograms to visualize continuous data
hist(d2$pswq)

hist(d2$pswq.1)

hist(d2$phq)

hist(d2$gad)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##         vars    n    mean      sd  median trimmed     mad min     max   range
## X          1 1165 4665.88 2575.36 4757.00 4714.85 3328.44   1 8867.00 8866.00
## gender*    2 1165    1.36    0.78    1.00    1.18    0.00   1    4.00    3.00
## health*    3 1165    8.61    1.37    9.00    8.92    0.00   1   11.00   10.00
## pswq       4 1165    2.75    0.80    2.79    2.75    0.95   1    4.94    3.94
## pswq.1     5 1165    2.75    0.80    2.79    2.75    0.95   1    4.94    3.94
## phq        6 1165    2.07    0.86    1.89    1.99    0.99   1    4.00    3.00
## gad        7 1165    2.04    0.91    1.71    1.94    0.85   1    4.00    3.00
##          skew kurtosis    se
## X       -0.13    -1.22 75.45
## gender*  1.86     1.84  0.02
## health* -2.27     5.73  0.04
## pswq     0.00    -0.80  0.02
## pswq.1   0.00    -0.80  0.02
## phq      0.65    -0.65  0.03
## gad      0.69    -0.70  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, health)
 health 
 cancer   diabetes   heart disease   high blood pressure   lung disease   other chronic health condition   other disability   prefer not to say   something else or not applicable   three or more conditions   two conditions 
 gender 
   I use another term  2 3 18 3 3
   Prefer not to say  1 1 1 1 14
   female  1 2 2 17 49 23 18 20 781 5 32
   male  1 5 5 4 5 4 137 1 6
   #Total cases  1 4 2 22 57 28 27 24 950 9 41
# 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$pswq, d2$gad,
     main="Scatterplot of WorryAdult and Anxiety",
     xlab = "WorryAdult",
     ylab = "Anxiety")

plot(d2$pswq.1, d2$phq,
     main="Scatterplot of WorryChild and PatientHealth",
     xlab = "WorryChild",
     ylab = "PatientHealth")

# 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~health,
        main="Boxplot of Health and PatienthealthQuestionnaire",
        xlab = "Health",
        ylab = "PatientHealthQuestionnaire")

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