# 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")
library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
##
## To get total summary skip 'by' argument: take_all(mtcars, mean)
# Import the "fakedata_2025.csv" file
d2 <- read.csv("Data/projectdata.csv")
str(d2)
## 'data.frame': 3078 obs. of 7 variables:
## $ ResponseID : chr "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
## $ npi : num 0.6923 0.1538 0.0769 0.0769 0.7692 ...
## $ efficacy : num 3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
## $ swb : num 4.33 4.17 1.83 5.17 3.67 ...
## $ moa_independence: num 3.67 3.67 3.5 3 3.83 ...
## $ usdream : chr "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
## $ edu : chr "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab
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$edu)
##
## 1 High school diploma or less, and NO COLLEGE
## 53
## 2 Currently in college
## 2491
## 3 Completed some college, but no longer in college
## 35
## 4 Complete 2 year College degree
## 177
## 5 Completed Bachelors Degree
## 135
## 6 Currently in graduate education
## 131
## 7 Completed some graduate degree
## 56
table(d2$usdream)
##
## american dream is important and achievable for me
## 1419
## american dream is important but maybe not achievable for me
## 337
## american dream is not important and maybe not achievable for me
## 568
## american dream is not important but is achievable for me
## 177
## not sure if american dream important
## 577
# use histograms to visualize continuous data (4 variables)
hist(d2$npi)
hist(d2$efficacy)
hist(d2$swb)
hist(d2$moa_independence)
describe(d2)
## vars n mean sd median trimmed mad min max
## ResponseID* 1 3078 1539.50 888.69 1539.50 1539.50 1140.86 1.0 3078
## npi 2 3078 0.28 0.31 0.15 0.24 0.23 0.0 1
## efficacy 3 3078 3.13 0.45 3.10 3.13 0.44 1.1 4
## swb 4 3078 4.47 1.32 4.67 4.53 1.48 1.0 7
## moa_independence 5 3078 3.54 0.47 3.67 3.61 0.49 1.0 4
## usdream* 6 3078 2.40 1.55 2.00 2.25 1.48 1.0 5
## edu* 7 3078 2.50 1.24 2.00 2.18 0.00 1.0 7
## range skew kurtosis se
## ResponseID* 3077.0 0.00 -1.20 16.02
## npi 1.0 0.94 -0.70 0.01
## efficacy 2.9 -0.26 0.48 0.01
## swb 6.0 -0.37 -0.46 0.02
## moa_independence 3.0 -1.44 2.53 0.01
## usdream* 4.0 0.62 -1.14 0.03
## edu* 6.0 2.20 3.75 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 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 3 were within the accepted range (-2/+2). However, 1 variable (moa_independence) was outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.
Crosstabs are used to visualize combinations of two categorical variables.
cross_cases(d2, edu, usdream)
|  usdream | |||||
|---|---|---|---|---|---|
|  american dream is important and achievable for me |  american dream is important but maybe not achievable for me |  american dream is not important and maybe not achievable for me |  american dream is not important but is achievable for me |  not sure if american dream important | |
|  edu | |||||
| Â Â Â 1 High school diploma or less, and NO COLLEGEÂ | 14 | 6 | 20 | 3 | 10 |
|    2 Currently in college | 1175 | 274 | 440 | 145 | 457 |
|    3 Completed some college, but no longer in college | 17 | 4 | 4 | 2 | 8 |
|    4 Complete 2 year College degree | 85 | 22 | 27 | 7 | 36 |
|    5 Completed Bachelors Degree | 56 | 14 | 26 | 6 | 33 |
|    6 Currently in graduate education | 50 | 12 | 35 | 11 | 23 |
|    7 Completed some graduate degree | 22 | 5 | 16 | 3 | 10 |
|    #Total cases | 1419 | 337 | 568 | 177 | 577 |
## 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(~ + , data=)
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$npi, d2$moa_independence,
main="Scatterplot of Narcissistic Personality Inventory and Markers of Adulthood importance– Independence",
xlab = "Narcissistic Personality Inventory",
ylab = "Markers of Adulthood importance– Independence ")
plot(d2$swb, d2$efficacy,
main="Scatterplot of satisfaction with life scale and general self efficacy",
xlab = "satisfaction with life scale",
ylab = "General self efficacy")
# 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 are used to visualize combinations of one categorical and one continuous variable.
# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable'
boxplot(data=d2, npi~edu,
main="Boxplot of by narcissistic personality inventory by education",
xlab = "Education",
ylab = "Narcissistic Personality Inventory")
boxplot(data=d2, swb~usdream,
main="Boxplot of Attainability of American Dream by Satisfaction with Life Scale",
xlab = "Attainability of American Dream",
ylab = "Satisfaction with Life Scale")
# 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!!