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)
## 
## 
## 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 "projectdata.csv" file

d2 <- read.csv("Project Folder 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 
##                1987                 116                  38                  18
table(d2$edu)
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 38 
##                             2 Currently in college 
##                                               1781 
## 3 Completed some college, but no longer in college 
##                                                 24 
##                   4 Complete 2 year College degree 
##                                                133 
##                       5 Completed Bachelors Degree 
##                                                 74 
##                  6 Currently in graduate education 
##                                                 78 
##                   7 Completed some graduate degree 
##                                                 31
# use histograms to visualize continuous data
hist(d2$socmeduse)

hist(d2$belong)

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 2159 1080.00 623.39 1080.0 1080.00 800.60  1.0 2159.0 2158.0
## age*           2 2159    1.11   0.43    1.0    1.00   0.00  1.0    4.0    3.0
## edu*           3 2159    2.44   1.15    2.0    2.13   0.00  1.0    7.0    6.0
## socmeduse      4 2159   34.25   8.59   35.0   34.52   7.41 11.0   55.0   44.0
## belong         5 2159    3.21   0.61    3.2    3.23   0.59  1.3    5.0    3.7
## stress         6 2159    3.07   0.60    3.1    3.07   0.59  1.3    4.6    3.3
## swb            7 2159    4.44   1.33    4.5    4.49   1.48  1.0    7.0    6.0
##              skew kurtosis    se
## ResponseId*  0.00    -1.20 13.42
## age*         4.41    21.05  0.01
## edu*         2.41     4.91  0.02
## socmeduse   -0.31     0.20  0.18
## belong      -0.27    -0.09  0.01
## stress      -0.02    -0.15  0.01
## swb         -0.35    -0.49  0.03
## 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 (#) were within the accepted range (-2/+2). However, (#) variables (list variable name 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, 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  33 1699 21 109 44 64 17
   2 between 26 and 35  4 61 1 14 19 9 8
   3 between 36 and 45  1 15 2 6 7 4 3
   4 over 45  6 4 4 1 3
   #Total cases  38 1781 24 133 74 78 31
# 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$stress, d2$swb,
     main="Scatterplot of stress and swb",
     xlab = "stress",
     ylab = "swb")

plot(d2$socmeduse, d2$belong,
     main="Scatterplot of secmeduse and belong",
     xlab = "socmeduse",
     ylab = "belong")

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

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.