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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
## 
## 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 "fakedata.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$relationship_status)
## 
##   In a relationship/married and cohabiting 
##                                          1 
## In a relationship/married but living apart 
##                                         67 
##                          Prefer not to say 
##                                         70 
##                Single, divorced or widowed 
##                                          2 
##                      Single, never married 
##                                        526
table(d2$pet)
## 
##                  bird                   cat           cat and dog 
##                     3                   119                    80 
##                   dog                  fish multiple types of pet 
##                   106                    20                    57 
##               no pets                 other 
##                   241                    40
# use histograms to visualize continuous data
hist(d2$mfq_26)

hist(d2$rse)

hist(d2$phq)

hist(d2$support)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##                      vars   n    mean      sd  median trimmed     mad  min
## X                       1 666 5148.34 2582.01 5706.00 5283.66 3043.78 20.0
## relationship_status*    2 666    4.48    1.04    5.00    4.72    0.00  1.0
## pet*                    3 666    5.04    2.10    6.00    5.10    2.97  1.0
## mfq_26                  4 666    4.24    0.65    4.25    4.25    0.63  1.8
## rse                     5 666    2.50    0.71    2.50    2.49    0.74  1.0
## phq                     6 666    2.28    0.85    2.22    2.23    0.99  1.0
## support                 7 666    3.48    0.93    3.50    3.51    0.99  1.0
##                         max range  skew kurtosis     se
## X                    8860.0  8840 -0.38    -1.14 100.05
## relationship_status*    5.0     4 -1.62     0.91   0.04
## pet*                    8.0     7 -0.23    -1.52   0.08
## mfq_26                  5.8     4 -0.27     0.11   0.03
## rse                     4.0     3  0.04    -0.77   0.03
## phq                     4.0     3  0.38    -0.87   0.03
## support                 5.0     4 -0.28    -0.64   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 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, relationship_status, pet)
 pet 
 bird   cat   cat and dog   dog   fish   multiple types of pet   no pets   other 
 relationship_status 
   In a relationship/married and cohabiting  1
   In a relationship/married but living apart  14 8 14 2 8 14 7
   Prefer not to say  10 7 9 4 4 34 2
   Single, divorced or widowed  1 1
   Single, never married  3 95 64 82 14 44 193 31
   #Total cases  3 119 80 106 20 57 241 40
# 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$mfq_26, d2$rse, 
     main="Scatterplot of Mental Flexibility Questionnaire (Trait) and Rosenberg Self-Esteem Inventory",
     xlab = "Mental Flexibility Questionnaire (Trait)",
     ylab = "Rosenberg Self-Esteem Inventory")

plot(d2$support, d2$phq,
     main="Scatterplot of Social Support Measure and Patient Health Questionnaire-9",
     xlab = "Social Support Measure",
     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

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

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

boxplot(data=d2, support~relationship_status,
        main="Boxplot of Relationship status and Social Support Measure",
        xlab = "Relationship status",
        ylab = "Social Support Measure")

boxplot(data=d2, rse~pet,
        main="Boxplot of Pets and Rosenberg Self-Esteem Inventory",
        xlab = "Pets",
        ylab = "Rosenberg Self-Esteem Inventory")

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