Basic Statistics

Load Libraries

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

#install.packages("psych")
#install.packages("expss") #new package

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.3.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.3.3
## 
## To aggregate several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  To return to the console output, use 'expss_output_default()'.

Import & Examine Data

# Import the "fakedata_2025.csv" file

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

str(d2)
## 'data.frame':    1000 obs. of  13 variables:
##  $ id            : chr  "id_1" "id_2" "id_3" "id_4" ...
##  $ exp_condition : chr  "level b" "level b" "level b" "level c" ...
##  $ grade         : chr  "level d" "level c" "level b" "level d" ...
##  $ family_type   : chr  "level a" "level a" "level b" "level b" ...
##  $ pet_type      : chr  "level b" "level c" "level b" NA ...
##  $ social_support: num  3.45 2.71 3.14 2.9 2.28 ...
##  $ happiness     : num  3.481 2.617 3.212 0.905 2.91 ...
##  $ loneliness    : num  1.04 2.28 1.85 1.36 1.34 ...
##  $ B5_consc      : num  4.46 4.75 3.52 4.62 3.22 ...
##  $ B5_neuro      : num  1.15 1.47 1.22 1.07 1.37 ...
##  $ B5_agree      : num  5.06 4.21 4.12 4.67 4.86 ...
##  $ B5_extro      : num  1.55 1.15 2.75 1.21 1.58 ...
##  $ B5_open       : num  6.87 7.21 5.25 5.85 6.94 ...
# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab

Univariate Plots: Histograms & Tables

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

# use tables to visualize categorical data (2 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 continuous data (4 variables)
hist(d2$social_support)

hist(d2$B5_consc)

hist(d2$B5_agree)

hist(d2$B5_extro)

Univariate Normality for Continuous Variables

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 continuous variables and all were within the accepted range (-2/+2).

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

Write-up of Normality

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

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, grade, family_type)
 family_type 
 level a   level b 
 grade 
   level a  5 29
   level b  65 192
   level c  84 289
   level d  48 193
   level e  16 37
   level f  1 2
   #Total cases  219 742
## Some students may have issues with this function working. If this happens to you, please try these 2 options:
## Option 1: install the "maditr" package and then call in its library.
## Option 2: If Option 1 doesn't work, then you will use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line.

xtabs(~ grade + family_type, data=d2)
##          family_type
## grade     level a level b
##   level a       5      29
##   level b      65     192
##   level c      84     289
##   level d      48     193
##   level e      16      37
##   level f       1       2
# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$social_support, d2$B5_consc,
     main="Scatterplot of Social Support and Conscientiousness",
     xlab = "Social Support",
     ylab = "Conscientiousness")

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 potential 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 construct names, NOT their R abbrev or full 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 = "Grade",
        ylab = "Social Support")

boxplot(data= d2, B5_extro~family_type,
        main="Boxplot of Extroversion by Family 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 potential hypotheses. You may repeat 1 variable to see its association with others. Again, 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 construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.

That’s it!!