# if you haven't used a given package before, you'll need to download it first
# delete the "#" before the install function and run it to download
# re-insert the "#" 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 select rows from data: rows(mtcars, am==0)
##
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
## To return to the console output, use 'expss_output_default()'.
##Import Data
# Import the "projectdata.csv" file
d2 <- read.csv("Data/projectdata.csv")
# 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
table(d2$income)
##
## 1 low 2 middle 3 high rather not say
## 870 878 533 843
table(d2$party_rc)
##
## apolitical democrat independent republican
## 432 1588 326 778
# use histograms to visualize continuous data
hist(d2$npi)
hist(d2$stress)
hist(d2$swb)
hist(d2$belong)
describe(d2)
## vars n mean sd median trimmed mad min max range
## ResponseId* 1 3124 1562.50 901.97 1562.50 1562.50 1157.91 1.0 3124.0 3123.0
## income* 2 3124 2.43 1.16 2.00 2.41 1.48 1.0 4.0 3.0
## party_rc* 3 3124 2.46 1.01 2.00 2.46 0.00 1.0 4.0 3.0
## npi 4 3124 0.28 0.31 0.15 0.24 0.23 0.0 1.0 1.0
## stress 5 3124 3.05 0.60 3.00 3.05 0.59 1.3 4.7 3.4
## swb 6 3124 4.48 1.32 4.67 4.53 1.48 1.0 7.0 6.0
## belong 7 3124 3.23 0.60 3.30 3.25 0.59 1.3 5.0 3.7
## skew kurtosis se
## ResponseId* 0.00 -1.20 16.14
## income* 0.15 -1.43 0.02
## party_rc* 0.42 -1.04 0.02
## npi 0.95 -0.68 0.01
## stress 0.03 -0.16 0.01
## swb -0.36 -0.46 0.02
## belong -0.25 -0.14 0.01
## For the required write-up below, choose one of these options to paste and edit below based on your output.
# We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).
# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name here) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.
We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).
Crosstabs are used to visualize combinations of two categorical variables.
cross_cases(d2, income, party_rc)
|  party_rc | ||||
|---|---|---|---|---|
|  apolitical |  democrat |  independent |  republican | |
|  income | ||||
|    1 low | 126 | 481 | 96 | 167 |
|    2 middle | 90 | 473 | 93 | 222 |
|    3 high | 41 | 243 | 42 | 207 |
|    rather not say | 175 | 391 | 95 | 182 |
|    #Total cases | 432 | 1588 | 326 | 778 |
find("cross_cases")
## [1] "package:expss"
# Note: for HW, replace the two variables with your project's categorical ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$swb, d2$belong,
main="Scatterplot of Satisfaction with Life Scale and Need to Belong Scale",
xlab = "swb",
ylab = "belong")
plot(d2$npi, d2$stress,
main="Scatterplot of Narcissistic Personality Inventory and Perceived Stress Questionairre",
xlab = "npi",
ylab = "stress")
# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your research questions/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.
Boxplots are used to visualize combinations of one categorical and one continuous variable.
# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable'
boxplot(data=d2, swb~income,
main="Boxplot of Satisfaction with Life and Income",
xlab = "income",
ylab = "swb")
boxplot(data=d2, npi~party_rc,
main="Boxplot of Political Party and Narcissistic Personality Inventory",
xlab = "party_rc",
ylab = "npi")
# 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 research questions/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 graph.