# 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
## Warning: package 'psych' was built under R version 4.4.3
library(expss) # for the cross_cases() command
## Warning: package 'expss' was built under R version 4.4.3
## Loading required package: maditr
## Warning: package 'maditr' was built under R version 4.4.3
##
## To select rows from data: rows(mtcars, am==0)
##
## 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 "fakedata.csv" file
d2 <- read.csv("C:\\Users\\maddi\\OneDrive\\Desktop\\Social psych lab\\final project\\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$gender)
##
## f m nb
## 1168 407 40
table(d2$party_rc)
##
## apolitical democrat independent republican
## 220 821 184 390
# use histograms to visualize continuous data
hist(d2$pipwd)
hist(d2$swb)
hist(d2$mindful)
hist(d2$efficacy)
describe(d2)
## vars n mean sd median trimmed mad min max range
## ResponseID* 1 1615 808.00 466.35 808.0 808.00 598.97 1.00 1615 1614.00
## gender* 2 1615 1.30 0.51 1.0 1.22 0.00 1.00 3 2.00
## party_rc* 3 1615 2.46 1.00 2.0 2.45 0.00 1.00 4 3.00
## pipwd 4 1615 2.93 0.56 3.0 2.93 0.40 1.13 5 3.87
## swb 5 1615 4.33 1.35 4.5 4.38 1.48 1.00 7 6.00
## mindful 6 1615 3.61 0.86 3.6 3.61 0.79 1.13 6 4.87
## efficacy 7 1615 3.11 0.46 3.1 3.12 0.44 1.10 4 2.90
## skew kurtosis se
## ResponseID* 0.00 -1.20 11.60
## gender* 1.41 1.01 0.01
## party_rc* 0.42 -1.01 0.02
## pipwd 0.12 1.35 0.01
## swb -0.30 -0.51 0.03
## mindful -0.03 -0.12 0.02
## efficacy -0.36 0.67 0.01
## 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.
We analyzed the skew and kurtosis of our continuous variables and (3) were within the accepted range (-2/+2). However, (1) variable, efficacy 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, gender, party_rc)
|  party_rc | ||||
|---|---|---|---|---|
|  apolitical |  democrat |  independent |  republican | |
|  gender | ||||
|    f | 158 | 614 | 126 | 270 |
|    m | 58 | 180 | 49 | 120 |
|    nb | 4 | 27 | 9 | |
|    #Total cases | 220 | 821 | 184 | 390 |
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$pipwd, d2$efficacy,
main="Scatterplot of pipwd and efficacy ",
xlab = "pipwd",
ylab = "efficacy")
plot(d2$mindful, d2$swb,
main="Scatterplot of mindful and efficacy ",
xlab = "mindful",
ylab = "efficacy")
# 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 are used to visualize combinations of one categorical and one continuous variable.
# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable'
boxplot(data=d2, efficacy~party_rc,
main="Boxplot of party_rc and efficacy",
xlab = "party_rc",
ylab = "efficacy")
boxplot(data=d2, swb~gender,
main="Boxplot of gender and swb ",
xlab = "gender",
ylab = "swb")
# 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.
We did it!!