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("psych")

library(psych) # for the describe() command
## Warning: package 'psych' was built under R version 4.5.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 
##                                                 59 
##                             2 Currently in college 
##                                               2553 
## 3 Completed some college, but no longer in college 
##                                                 35 
##                   4 Complete 2 year College degree 
##                                                182 
##                       5 Completed Bachelors Degree 
##                                                139 
##                  6 Currently in graduate education 
##                                                136 
##                   7 Completed some graduate degree 
##                                                 59
table(d2$sibling)
## 
## at least one sibling           only child 
##                 2860                  303
# use histograms to visualize 4 continuous variables
hist(d2$swb)

hist(d2$belong)

hist(d2$socmeduse)

hist(d2$exploit)

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 3163 1582.00 913.22 1582.00 1582.00 1172.74  1.0 3163 3162.0
## edu*           2 3163    2.50   1.25    2.00    2.18    0.00  1.0    7    6.0
## sibling*       3 3163    1.10   0.29    1.00    1.00    0.00  1.0    2    1.0
## swb            4 3163    4.48   1.32    4.67    4.53    1.48  1.0    7    6.0
## belong         5 3163    3.23   0.61    3.30    3.25    0.59  1.3    5    3.7
## socmeduse      6 3163   34.45   8.57   35.00   34.72    7.41 11.0   55   44.0
## exploit        7 3163    2.39   1.37    2.00    2.21    1.48  1.0    7    6.0
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.24
## edu*         2.19     3.69  0.02
## sibling*     2.75     5.54  0.01
## swb         -0.36    -0.45  0.02
## belong      -0.26    -0.12  0.01
## socmeduse   -0.31     0.26  0.15
## exploit      0.94     0.36  0.02
## 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.98141, p-value < 0.00000000000000022
shapiro.test(d2$belong)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$belong
## W = 0.99245, p-value = 0.000000000007787
shapiro.test(d2$socmeduse)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$socmeduse
## W = 0.98779, p-value = 0.0000000000000007093
shapiro.test(d2$exploit)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$exploit
## W = 0.88307, p-value < 0.00000000000000022

I analyzed the skew and kurtosis of our four continuous variables and (3) were within the acceptable range (-2/+2). However, (1) variables (social media use) were 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$socmeduse, d2$belong, 
     main="Scatterplot of Social Media Use and Need to Belong",
     xlab = "Social Media Use",
     ylab = "Need to Belong")

plot(d2$swb, d2$exploit,
     main="Scatterplot of Satisfaction with Life and Interpersonal Exploitativeness",
     xlab = "Satisfaction with Life",
     ylab = "Interpersonal Exploitativeness")

# 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, belong~sibling,
        main="Boxplot of Need to Belong by Sibling Status ",
        xlab = "Sibling Status",
        ylab = "Need to Belong")

boxplot(data=d2, socmeduse~edu,
        main="Boxplot of Social Media Use by Education Level ",
        xlab = "Education Level",
        ylab = "Social Media Use")

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