# 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 aggregate all non-grouping columns: take_all(mtcars, mean, by = am)
##
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
##
## sort_by
##
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
## To return to the console output, use 'expss_output_default()'.
##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$age)
##
## 1 under 18 2 between 18 and 25 3 between 26 and 35 4 between 36 and 45
## 599 53 6 86
## 5 over 45
## 173
table(d2$urban_rural)
##
## city isolated dwelling town village
## 203 20 404 290
# use histograms to visualize continuous data
hist(d2$big5_con)
hist(d2$phq)
hist(d2$support)
hist(d2$swemws)
# Replace these with the variable names for the HW
describe(d2)
## vars n mean sd median trimmed mad min max range
## X 1 917 4669.95 2576.09 4778.00 4715.90 3300.27 20 8860 8840
## age* 2 917 2.11 1.65 1.00 1.89 0.00 1 5 4
## urban_rural* 3 917 2.85 1.10 3.00 2.94 1.48 1 4 3
## big5_con 4 917 4.84 1.20 5.00 4.87 1.48 1 7 6
## phq 5 917 2.05 0.85 1.89 1.97 0.99 1 4 3
## support 6 917 3.61 0.94 3.67 3.66 0.99 1 5 4
## swemws 7 917 3.18 0.84 3.29 3.19 0.85 1 5 4
## skew kurtosis se
## X -0.11 -1.21 85.07
## age* 0.96 -0.92 0.05
## urban_rural* -0.71 -0.81 0.04
## big5_con -0.27 -0.31 0.04
## phq 0.66 -0.61 0.03
## support -0.45 -0.56 0.03
## swemws -0.19 -0.38 0.03
## 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, age, urban_rural)
|  urban_rural | ||||
|---|---|---|---|---|
|  city |  isolated dwelling |  town |  village | |
|  age | ||||
| Â Â Â 1 under 18Â | 138 | 11 | 265 | 185 |
| Â Â Â 2 between 18 and 25Â | 19 | 4 | 16 | 14 |
| Â Â Â 3 between 26 and 35Â | 1 | 2 | 3 | |
| Â Â Â 4 between 36 and 45Â | 11 | 4 | 45 | 26 |
| Â Â Â 5 over 45Â | 34 | 1 | 76 | 62 |
|    #Total cases | 203 | 20 | 404 | 290 |
# 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$phq, d2$swemws,
main="Scatterplot of phq and swemws",
xlab = "phq",
ylab = "swemws")
# The variable you put first will go on the xlab, 2nd variable ylab
plot(d2$big5_con, d2$swemws,
main="Scatterplot of big5_con and swemws",
xlab = "big5_con",
ylab = "swemws")
# 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, phq~age,
main="Boxplot of age and phq",
xlab = "age",
ylab = "phq")
boxplot(data=d2, support~age,
main="Boxplot of age and support",
xlab = "age",
ylab = "support")
# 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.