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

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 
##                1901                 110                  37                  17
table(d2$income)
## 
##          1 low       2 middle         3 high rather not say 
##            572            599            335            559
# use histograms to visualize 4 continuous variables
hist(d2$moa_independence)

hist(d2$moa_safety)

hist(d2$efficacy)

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
## ResponseID*         1 2065 1033.00 596.26 1033.00 1033.00 765.02 1.0 2065.0
## age*                2 2065    1.11   0.43    1.00    1.00   0.00 1.0    4.0
## income*             3 2065    2.43   1.16    2.00    2.41   1.48 1.0    4.0
## moa_independence    4 2065    3.54   0.47    3.67    3.61   0.49 1.0    4.0
## moa_safety          5 2065    3.21   0.65    3.25    3.28   0.74 1.0    4.0
## efficacy            6 2065    3.11   0.44    3.10    3.12   0.44 1.2    4.0
## stress              7 2065    3.07   0.60    3.10    3.07   0.59 1.3    4.6
##                   range  skew kurtosis    se
## ResponseID*      2064.0  0.00    -1.20 13.12
## age*                3.0  4.41    20.98  0.01
## income*             3.0  0.17    -1.42  0.03
## moa_independence    3.0 -1.50     2.78  0.01
## moa_safety          3.0 -0.71    -0.08  0.01
## efficacy            2.8 -0.21     0.40  0.01
## stress              3.3 -0.02    -0.14  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$moa_independence)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$moa_independence
## W = 0.85014, p-value < 0.00000000000000022
shapiro.test(d2$moa_safety)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$moa_safety
## W = 0.92505, p-value < 0.00000000000000022
shapiro.test(d2$efficacy)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$efficacy
## W = 0.98081, p-value = 0.0000000000000004778
shapiro.test(d2$stress)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$stress
## W = 0.99602, p-value = 0.00002863

We analyzed the skew and kurtosis of our four continuous variables and (three) were within the acceptable range (-2/+2). However, (one) variable (moa_independence) was outside of the acceptable range. For our analysis we will use them anyway, but outside of class this is bad practice.

Bivariate Plots

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$efficacy, d2$stress,
     main="Scatterplot of Self Efficacy and Perceived Stress",
     xlab = "Self Efficacy",
     ylab = "Perceived Stress")

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

# 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, stress~income,
        main="Boxplot of Stress by Household Income",
        xlab = "Household Income",
        ylab = "Stress")

boxplot(data=d2, efficacy~age,
        main="Boxplot of Self Efficacy by Age",
        xlab = "Age",
        ylab = "Self Efficacy")

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