# 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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
##Import Data
# Import the "fakedata.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$gender)
##
## f m nb
## 2295 783 54
table(d2$sibling)
##
## at least one sibling only child
## 2831 301
# use histograms to visualize continuous data
hist(d2$moa_maturity)
hist(d2$exploit)
hist(d2$swb)
hist(d2$mindful)
describe(d2)
## vars n mean sd median trimmed mad min max range
## ResponseID* 1 3132 1566.50 904.27 1566.50 1566.50 1160.88 1.00 3132 3131.00
## gender* 2 3132 1.28 0.49 1.00 1.21 0.00 1.00 3 2.00
## sibling* 3 3132 1.10 0.29 1.00 1.00 0.00 1.00 2 1.00
## moa_maturity 4 3132 3.59 0.43 3.67 3.65 0.49 1.00 4 3.00
## exploit 5 3132 2.39 1.37 2.00 2.21 1.48 1.00 7 6.00
## swb 6 3132 4.47 1.32 4.67 4.53 1.48 1.00 7 6.00
## mindful 7 3132 3.71 0.84 3.73 3.71 0.79 1.13 6 4.87
## skew kurtosis se
## ResponseID* 0.00 -1.20 16.16
## gender* 1.39 0.87 0.01
## sibling* 2.74 5.51 0.01
## moa_maturity -1.20 1.87 0.01
## exploit 0.94 0.36 0.02
## swb -0.36 -0.45 0.02
## mindful -0.06 -0.13 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.
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, gender, sibling)
|  sibling | ||
|---|---|---|
|  at least one sibling |  only child | |
|  gender | ||
|    f | 2081 | 214 |
|    m | 701 | 82 |
|    nb | 49 | 5 |
|    #Total cases | 2831 | 301 |
# Note: for HW, replace the two lab variables with your project ones)
Scatterplots are used to visualize combinations of two continuous variables.
plot(d2$moa_maturity, d2$exploit,
main="Scatterplot of Maturity and Interpersonal Exploitativeness",
xlab = "Maturity",
ylab = "Interpersonal Exploitativeness")
plot(d2$swb, d2$mindful,
main="Scatterplot of Subjective Well-being and Mindfulness ",
xlab = "Subjective Well-Being",
ylab = "Mindfulness")
# 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, moa_maturity~gender,
main="Boxplot of Gender Identity and Maturity",
xlab = "Gender Identity",
ylab = "Maturity")
boxplot(data=d2, mindful~sibling,
main="Boxplot of Sibling and Mindfulness",
xlab = "Sibling",
ylab = "Mindfulness")
# 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!!