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)
## 
## 
## 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 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                1926                 113                  37                  17
table(d2$edu)
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 34 
##                             2 Currently in college 
##                                               1729 
## 3 Completed some college, but no longer in college 
##                                                 23 
##                   4 Complete 2 year College degree 
##                                                128 
##                       5 Completed Bachelors Degree 
##                                                 75 
##                  6 Currently in graduate education 
##                                                 75 
##                   7 Completed some graduate degree 
##                                                 29
# use histograms to visualize continuous data
hist(d2$stress)

hist(d2$moa_independence)

hist(d2$moa_maturity)

hist(d2$swb)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##                  vars    n    mean     sd  median trimmed    mad  min    max
## ResponseID*         1 2093 1047.00 604.34 1047.00 1047.00 775.40 1.00 2093.0
## age*                2 2093    1.11   0.43    1.00    1.00   0.00 1.00    4.0
## edu*                3 2093    2.44   1.15    2.00    2.13   0.00 1.00    7.0
## stress              4 2093    3.07   0.60    3.10    3.07   0.59 1.30    4.6
## moa_independence    5 2093    3.54   0.47    3.67    3.61   0.49 1.00    4.0
## moa_maturity        6 2093    3.61   0.43    3.67    3.67   0.49 1.33    4.0
## swb                 7 2093    4.43   1.33    4.50    4.49   1.48 1.00    7.0
##                    range  skew kurtosis    se
## ResponseID*      2092.00  0.00    -1.20 13.21
## age*                3.00  4.40    21.00  0.01
## edu*                6.00  2.40     4.87  0.03
## stress              3.30 -0.02    -0.15  0.01
## moa_independence    3.00 -1.50     2.79  0.01
## moa_maturity        2.67 -1.23     1.69  0.01
## swb                 6.00 -0.36    -0.49  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 3 were within the accepted range (-2/+2). However, 1 variable (Independence) was 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, 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 
 age 
   1 between 18 and 25  29 1651 20 104 45 61 16
   2 between 26 and 35  4 58 1 14 19 9 8
   3 between 36 and 45  1 14 2 6 7 4 3
   4 over 45  6 4 4 1 2
   #Total cases  34 1729 23 128 75 75 29
# 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$stress, d2$swb, 
     main="Scatterplot of Stress and Satisfaction with Life",
     xlab = "Stress",
     ylab = "Satisfaction with Life")

plot(d2$moa_independence, d2$moa_maturity, 
     main="Scatterplot of Independence and Maturity",
     xlab = "Independence",
     ylab = "Maturity")

# 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, swb ~ edu,
        main="Boxplot of Education and Satisfaction with Life",
        xlab = "Education",
        ylab = "Satisfaction with Life")

boxplot(data=d2, moa_maturity ~ age,
        main="Boxplot of Age and Maturity",
        xlab = "Age",
        ylab = "Maturity")

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