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
## Warning: package 'psych' was built under R version 4.3.3

##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$edu)
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 53 
##                             2 Currently in college 
##                                               2496 
## 3 Completed some college, but no longer in college 
##                                                 35 
##                   4 Complete 2 year College degree 
##                                                178 
##                       5 Completed Bachelors Degree 
##                                                136 
##                  6 Currently in graduate education 
##                                                131 
##                   7 Completed some graduate degree 
##                                                 56
table(d2$usdream)
## 
##               american dream is important and achievable for me 
##                                                            1424 
##     american dream is important but maybe not achievable for me 
##                                                             337 
## american dream is not important and maybe not achievable for me 
##                                                             570 
##        american dream is not important but is achievable for me 
##                                                             177 
##                            not sure if american dream important 
##                                                             577
# use histograms to visualize 4 continuous variables
hist(d2$swb)

hist(d2$moa_independence)

hist(d2$belong)

hist(d2$efficacy)

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
## ResponseID*         1 3085 1543.00 890.71 1543.00 1543.00 1143.08 1.0 3085
## edu*                2 3085    2.50   1.24    2.00    2.18    0.00 1.0    7
## usdream*            3 3085    2.40   1.55    2.00    2.25    1.48 1.0    5
## swb                 4 3085    4.47   1.32    4.67    4.53    1.48 1.0    7
## moa_independence    5 3085    3.54   0.46    3.67    3.61    0.49 1.0    4
## belong              6 3085    3.24   0.61    3.30    3.25    0.59 1.3    5
## efficacy            7 3085    3.13   0.45    3.10    3.13    0.44 1.1    4
##                   range  skew kurtosis    se
## ResponseID*      3084.0  0.00    -1.20 16.04
## edu*                6.0  2.20     3.74  0.02
## usdream*            4.0  0.62    -1.14  0.03
## swb                 6.0 -0.37    -0.45  0.02
## moa_independence    3.0 -1.45     2.54  0.01
## belong              3.7 -0.26    -0.14  0.01
## efficacy            2.9 -0.25     0.47  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 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.98092, p-value < 2.2e-16
shapiro.test(d2$moa_independence)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$moa_independence
## W = 0.85561, p-value < 2.2e-16
shapiro.test(d2$belong)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$belong
## W = 0.99249, p-value = 1.332e-11
shapiro.test(d2$efficacy)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$efficacy
## W = 0.97898, p-value < 2.2e-16

[TYPE Write-up of Normality here] 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$moa_independence,
     main="Scatterplot of Satisfaction with Life and Independence ",
     xlab = "Satisfaction with Life",
     ylab = "Independence")

plot(d2$belong, d2$efficacy,
     main="Scatterplot of Need to Belong and General Self Efficacy",
     xlab = "Need to Belong",
     ylab = "Self Efficacy")

# 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 Satisfaction with Life by Education",
        xlab = "Education",
        ylab = "Satisfaction with Life")

boxplot(data=d2, belong~usdream,
        main="Boxplot of Attainability of the American Dream by Need to Belong",
        xlab = "Need to Belong",
        ylab = "Attainability of The American Dream")

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