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 get total summary skip 'by' argument: take_all(mtcars, mean)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

##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$party_rc)
## 
##  apolitical    democrat independent  republican 
##         438        1596         326         781
table(d2$edu)
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 57 
##                             2 Currently in college 
##                                               2541 
## 3 Completed some college, but no longer in college 
##                                                 35 
##                   4 Complete 2 year College degree 
##                                                178 
##                       5 Completed Bachelors Degree 
##                                                137 
##                  6 Currently in graduate education 
##                                                134 
##                   7 Completed some graduate degree 
##                                                 59
# use histograms to visualize continuous data
hist(d2$npi)

hist(d2$exploit)

hist(d2$stress)

hist(d2$swb)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars    n    mean     sd  median trimmed     mad min    max  range
## ResponseId*    1 3141 1571.00 906.87 1571.00 1571.00 1163.84 1.0 3141.0 3140.0
## npi            2 3141    0.28   0.31    0.15    0.24    0.23 0.0    1.0    1.0
## exploit        3 3141    2.39   1.37    2.00    2.21    1.48 1.0    7.0    6.0
## stress         4 3141    3.05   0.60    3.00    3.05    0.59 1.3    4.7    3.4
## swb            5 3141    4.48   1.32    4.67    4.53    1.48 1.0    7.0    6.0
## party_rc*      6 3141    2.46   1.01    2.00    2.45    0.00 1.0    4.0    3.0
## edu*           7 3141    2.50   1.24    2.00    2.17    0.00 1.0    7.0    6.0
##              skew kurtosis    se
## ResponseId*  0.00    -1.20 16.18
## npi          0.94    -0.69  0.01
## exploit      0.94     0.36  0.02
## stress       0.03    -0.17  0.01
## swb         -0.36    -0.46  0.02
## party_rc*    0.42    -1.04  0.02
## edu*         2.20     3.75  0.02
## 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 variable (education) was 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). We analyzed the skew and kurtosis of our continuous variables and 5 were within the accepted range (-2/+2). However, 1 variable (education) was outside of the accepted range. For this analysis, we will use it 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, party_rc, edu)
 edu 
 1 High school diploma or less, and NO COLLEGE   2 Currently in college   3 Completed some college, but no longer in college   4 Complete 2 year College degree   5 Completed Bachelors Degree   6 Currently in graduate education   7 Completed some graduate degree 
 party_rc 
   apolitical  13 339 6 28 19 26 7
   democrat  27 1271 17 86 78 77 40
   independent  4 260 6 27 11 13 5
   republican  13 671 6 37 29 18 7
   #Total cases  57 2541 35 178 137 134 59
# 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$npi, d2$exploit,
     main="Scatterplot of npi and exploit",
     xlab = "npi",
     ylab = "exploit")

plot(d2$stress, d2$swb,
     main="Scatterplot of stress and swb",
     xlab = "stress",
     ylab = "swb")

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

boxplot(data=d2, swb~edu,
        main="Boxplot of swb and edu",
        xlab = "swb",
        ylab = "edu")

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