Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# insert the "#" before the install function so that the file will Knit later
# then run the library function calling that package

#install.packages("")

library(psych) # for the describe() command

Import Data

# Import the "fakedata.csv" file for this Lab

d2 <- read.csv("Data/projectdata.csv")

# Note: for your Project version, you will import "projectdata.csv" that you created and exported at the end of the Project Data Prep assignment.

Univariate Plots: Histograms & Tables

Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.

# use tables to visualize 2 categorical variables
table(d2$age)
## 
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                1980                 112                  38                  18
table(d2$party_rc)
## 
##  apolitical    democrat independent  republican 
##         327        1096         219         506
# use histograms to visualize 4 continuous variables
hist(d2$swb)

hist(d2$support)

hist(d2$socmeduse)

hist(d2$stress)

Univariate Normality for Continuous Variables (individually)

# quick & dirty normality test: check skew & kurtosis values
describe(d2)
##             vars    n    mean     sd  median trimmed    mad  min    max  range
## ResponseID*    1 2148 1074.50 620.22 1074.50 1074.50 796.16  1.0 2148.0 2147.0
## age*           2 2148    1.11   0.43    1.00    1.00   0.00  1.0    4.0    3.0
## party_rc*      3 2148    2.42   1.01    2.00    2.40   0.00  1.0    4.0    3.0
## swb            4 2148    4.44   1.33    4.50    4.50   1.48  1.0    7.0    6.0
## support        5 2148    5.53   1.13    5.75    5.66   0.99  0.0    7.0    7.0
## socmeduse      6 2148   34.25   8.60   35.00   34.52   7.41 11.0   55.0   44.0
## stress         7 2148    3.06   0.60    3.10    3.06   0.59  1.3    4.6    3.3
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 13.38
## age*         4.44    21.34  0.01
## party_rc*    0.46    -0.97  0.02
## swb         -0.36    -0.49  0.03
## support     -1.09     1.33  0.02
## socmeduse   -0.31     0.20  0.19
## stress      -0.02    -0.15  0.01
## 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 four continuous variables and all were within the acceptable range (-2/+2).

## OPTION 2
# We analyzed the skew and kurtosis of our four continuous variables and (#) were within the acceptable range (-2/+2). However, (#) variables (list variable construct name(s) here) were outside of the acceptable range. For our analysis we will use them anyway, but outside of  class this is bad practice.

# Objective normality test: Shapiro-Wilks test
# We want the test to be NON-SIG (aka > .05) to indicate normality

options(scipen = 999)
# the above code turns off scientific notation

shapiro.test(d2$swb)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$swb
## W = 0.98097, p-value = 0.0000000000000002459
shapiro.test(d2$support)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$support
## W = 0.92386, p-value < 0.00000000000000022
shapiro.test(d2$socmeduse)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$socmeduse
## W = 0.98833, p-value = 0.000000000003258
shapiro.test(d2$stress)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$stress
## W = 0.996, p-value = 0.0000179

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

Bivariate Plots

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$swb, d2$support,
     main="Scatterplot Satisfaction With Life and Social Support",
     xlab = "Satisfaction With Life",
     ylab = "Social Support")

plot(d2$socmeduse, d2$stress,
     main="Scatterplot of Social Media Use and Stress",
     xlab = "Social Media Use",
     ylab = "Stress")

# 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~age,
        main="Boxplot of Satisfaction With Life by Age",
        xlab = "Age",
        ylab = "Satisfaction With Life")

boxplot(data=d2, stress~party_rc,
        main="Boxplot of Stress by Political Party",
        xlab = "Political Party",
        ylab = "Stress")

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

Those are the basics!!