Basic Statistics

Load Libraries

# 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("expss")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## Use magrittr pipe '%>%' to chain several operations:
##              mtcars %>%
##                  let(mpg_hp = mpg/hp) %>%
##                  take(mean(mpg_hp), by = am)
## 
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

Import & Examine Data

# Import the "projectdata.csv" file

d2 <- read.csv("Data/projectdata.csv")

str(d2)
## 'data.frame':    3147 obs. of  7 variables:
##  $ ResponseID: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ marriage5 : chr  "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
##  $ gender    : chr  "f" "m" "m" "f" ...
##  $ socmeduse : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ support   : num  6 6.75 5.17 5.58 6 ...
##  $ mindful   : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ npi       : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab

Univariate Plots: Histograms & Tables

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$gender)
## 
##    f    m   nb 
## 2309  784   54
table(d2$marriage5)
## 
##             are currently divorced from one another 
##                                                 733 
##                are currently married to one another 
##                                                2121 
##       never married each other and are not together 
##                                                 246 
## never married each other but are currently together 
##                                                  47
# use histograms to visualize continuous data (4 variables)
hist(d2$socmeduse)

hist(d2$mindful)

hist(d2$support)

hist(d2$npi)

Univariate Normality for Continuous Variables

describe(d2)
##             vars    n    mean     sd  median trimmed     mad   min  max   range
## ResponseID*    1 3147 1574.00 908.60 1574.00 1574.00 1166.81  1.00 3147 3146.00
## marriage5*     2 3147    1.88   0.60    2.00    1.83    0.00  1.00    4    3.00
## gender*        3 3147    1.28   0.49    1.00    1.21    0.00  1.00    3    2.00
## socmeduse      4 3147   34.45   8.55   35.00   34.73    7.41 11.00   55   44.00
## support        5 3147    5.54   1.13    5.75    5.66    0.99  0.00    7    7.00
## mindful        6 3147    3.71   0.84    3.73    3.72    0.79  1.13    6    4.87
## npi            7 3147    0.28   0.31    0.15    0.24    0.23  0.00    1    1.00
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.20
## marriage5*   0.47     1.49  0.01
## gender*      1.40     0.89  0.01
## socmeduse   -0.32     0.27  0.15
## support     -1.11     1.46  0.02
## mindful     -0.06    -0.14  0.02
## npi          0.94    -0.69  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.

Write-up of Normality

We analyzed the skew and kurtosis of our continuous variables and all were within the accepted range (-2/+2).

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, marriage5, gender)
 gender 
 f   m   nb 
 marriage5 
   are currently divorced from one another  546 173 14
   are currently married to one another  1546 541 34
   never married each other and are not together  183 57 6
   never married each other but are currently together  34 13
   #Total cases  2309 784 54
## 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

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$mindful, d2$npi,
     main="Scatterplot of Mindfulness and Narcissistic Personality",
     xlab = "Mindfulness",
     ylab = "Narcissistic Personality")

plot(d2$support, d2$socmeduse, 
     main="Scatterplot of Social Support and Social Media Use",
     xlab = "Social Media Use",
     ylab = "Social Support")

# 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

Boxplots are used to visualize combinations of one categorical and one continuous variable.

# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable' 

boxplot(data= d2, npi~gender,
        main="Boxplot of Narcissistic Personality by Gender",
        xlab = "Gender",
        ylab = "Narcissistic Personality")

boxplot(data= d2, mindful ~marriage5, 
        main="Boxplot of Mindfulness by Parent'a Marital Status ",
        xlab = "Parent's Marital Status",
        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 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!!