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/fakedata_2025.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$grade)
## 
## level a level b level c level d level e level f 
##      36     261     379     247      53       4
table(d2$family_type)
## 
## level a level b 
##     220     760
# use histograms to visualize 4 continuous variables
hist(d2$social_support)

hist(d2$B5_agree)

hist(d2$B5_consc)

hist(d2$B5_extro)

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
## id*               1 1000 500.50 288.82 500.50  500.50 370.65 1.00 1000.00
## exp_condition*    2  980   2.01   0.66   2.00    2.02   0.00 1.00    3.00
## grade*            3  980   3.03   0.96   3.00    3.01   1.48 1.00    6.00
## family_type*      4  980   1.78   0.42   2.00    1.84   0.00 1.00    2.00
## pet_type*         5  666   2.54   0.60   3.00    2.61   0.00 1.00    3.00
## social_support    6  980   2.52   0.49   2.50    2.51   0.50 1.09    4.15
## happiness         7  980   2.99   0.73   2.98    2.99   0.74 0.80    4.97
## loneliness        8  980   1.63   0.40   1.59    1.60   0.42 1.00    3.44
## B5_consc          9  980   3.88   0.65   3.96    3.92   0.68 1.36    5.00
## B5_neuro         10  980   1.28   0.19   1.25    1.27   0.19 1.00    2.15
## B5_agree         11  980   4.87   0.97   4.90    4.89   0.96 1.04    6.98
## B5_extro         12  980   1.81   0.61   1.70    1.75   0.64 1.00    3.99
## B5_open          13  980   4.15   1.89   4.13    4.11   2.02 0.17    9.91
##                 range  skew kurtosis   se
## id*            999.00  0.00    -1.20 9.13
## exp_condition*   2.00 -0.01    -0.74 0.02
## grade*           5.00  0.16    -0.30 0.03
## family_type*     1.00 -1.32    -0.26 0.01
## pet_type*        2.00 -0.91    -0.18 0.02
## social_support   3.06  0.12    -0.06 0.02
## happiness        4.18 -0.07    -0.18 0.02
## loneliness       2.44  0.70     0.35 0.01
## B5_consc         3.63 -0.59     0.08 0.02
## B5_neuro         1.15  0.74     0.38 0.01
## B5_agree         5.94 -0.33     0.18 0.03
## B5_extro         2.99  0.82     0.13 0.02
## B5_open          9.74  0.19    -0.45 0.06
## 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$social_support)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$social_support
## W = 0.99788, p-value = 0.2507
shapiro.test(d2$B5_agree)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$B5_agree
## W = 0.99124, p-value = 0.00001436
shapiro.test(d2$B5_consc)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$B5_consc
## W = 0.97233, p-value = 0.000000000001035
shapiro.test(d2$B5_extro)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$B5_extro
## W = 0.93418, p-value < 0.00000000000000022

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$social_support, d2$B5_consc,
     main="Scatterplot of Social Support and Conscientousness ",
     xlab = "Social Support",
     ylab = "Conscientousness")

plot(d2$B5_agree, d2$B5_extro,
     main="Scatterplot of Agreeableness and Extroversion",
     xlab = "Agreeableness",
     ylab = "Extroversion")

# 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, social_support~grade,
        main="Boxplot of Social Support by Grade",
        xlab = "Letter Grade",
        ylab = "Social Support")

boxplot(data=d2, B5_extro~family_type,
        main="Boxplot of Extroversion by Familt Type ",
        xlab = "Family Type",
        ylab = "Extroversion")

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