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 select rows from data: rows(mtcars, am==0)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## 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$age)
## 
##          1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45 
##                 871                  80                  14                 129 
##           5 over 45 
##                 243
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                           149                             8 
##                    depression              eating disorders 
##                            38                            31 
##                    none or NA obsessive compulsive disorder 
##                          1028                            27 
##                         other                          ptsd 
##                            33                            23
# use histograms to visualize continuous data
hist(d2$covid_pos)

hist(d2$covid_neg)

hist(d2$big5_open)

hist(d2$big5_ext)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##           vars    n    mean      sd  median trimmed     mad min  max range
## X            1 1337 4865.73 2578.34 5195.00 4957.45 3236.52  20 8860  8840
## age*         2 1337    2.10    1.64    1.00    1.87    0.00   1    5     4
## mhealth*     3 1337    4.58    1.44    5.00    4.82    0.00   1    8     7
## covid_pos    4 1337    2.05    3.38    0.00    1.32    0.00   0   15    15
## covid_neg    5 1337    1.21    1.87    0.00    0.85    0.00   0    8     8
## big5_open    6 1337    5.21    1.13    5.33    5.29    0.99   1    7     6
## big5_ext     7 1337    4.37    1.45    4.33    4.41    1.48   1    7     6
##            skew kurtosis    se
## X         -0.24    -1.19 70.51
## age*       0.97    -0.89  0.04
## mhealth*  -1.40     2.13  0.04
## covid_pos  1.58     1.47  0.09
## covid_neg  1.29     0.44  0.05
## big5_open -0.75     0.54  0.03
## big5_ext  -0.24    -0.78  0.04
## For the required write-up below, choose one of these options to paste and edit below based on your output.

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

# We analyzed the skew and kurtosis of our continuous variables and (5) were within the accepted range (-2/+2). However, (1) variables (mhealth) 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 (5) were within the accepted range (-2/+2). However, (1) variables (mhealth) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, age, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 age 
   1 under 18  106 4 16 25 662 20 23 15
   2 between 18 and 25  12 1 1 5 50 5 3 3
   3 between 26 and 35  3 11
   4 between 36 and 45  17 2 10 1 92 2 4 1
   5 over 45  11 1 11 213 3 4
   #Total cases  149 8 38 31 1028 27 33 23
# Note: for HW, replace the two variables with your project's categorical ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$covid_pos, d2$covid_neg,
     main="Scatterplot of covid_pos and covid_neg",
     xlab = "covid_pos",
     ylab = "covid_neg") 

plot(d2$big5_open, d2$big5_ext,
     main="Scatterplot of big5_open and big5_ext",
     xlab = "big5_open", 
     ylab = "big5_ext")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your research questions/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.

Boxplots

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

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

boxplot(data=d2, covid_pos~age,
        main="Boxplot of Variable2 and Variable5",
        xlab = "age",
        ylab = "covid_pos")

boxplot(data=d2, big5_open~mhealth,
        main="Boxplot of mhealth and big5_open",
        xlab = "mhealth",
        ylab = "big5_open")

# 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 research questions/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 graph.