0. Code to run to set up your computer.
# Update Packages
# update.packages(ask = FALSE, repos='https://cran.csiro.au/', dependencies = TRUE)
# Install Packages
if(!require(dplyr)) {install.packages("sjlabelled", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(sjlabelled)) {install.packages("sjlabelled", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(sjmisc)) {install.packages("sjmisc", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(sjstats)) {install.packages("sjstats", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(sjPlot)) {install.packages("sjlabelled", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(summarytools)) {install.packages("summarytools", repos='https://cran.csiro.au/', dependencies=TRUE)}
if(!require(ggplot2)) {install.packages("ggplot2", repos='https://cran.csiro.au/', dependencies= TRUE)}
if(!require(ggthemes)) {install.packages("ggthemes", repos='https://cran.csiro.au/', dependencies= TRUE)}
if (!require(GPArotation)) install.packages("GPArotation", repos='https://cran.csiro.au/', dependencies = TRUE)
if (!require(psych)) install.packages("psych", repos='https://cran.csiro.au/', dependencies = TRUE)
if (!require(ggrepel)) install.packages("ggrepel", repos='https://cran.csiro.au/', dependencies = TRUE)
# Load packages into memory
library(dplyr)
library(sjlabelled)
library(sjmisc)
library(sjstats)
library(sjPlot)
library(summarytools)
library(ggplot2)
library(ggthemes)
library(GPArotation)
library(psych)
library(ggrepel)
# Turn off scientific notation
options(digits=5, scipen=15)
# Stop View from overloading memory with a large datasets
RStudioView <- View
View <- function(x) {
if ("data.frame" %in% class(x)) { RStudioView(x[1:500,]) } else { RStudioView(x) }
}
# Datasets
# Example 1: Crime Dataset
lga <- readRDS(url("https://methods101.com/data/nsw-lga-crime-clean.RDS"))
# extract just the crimes from crime dataset
first <- which( colnames(lga)=="astdomviol" )
last <- which(colnames(lga)=="transport")
crimes <- lga[, first:last ]
# Example 2: AuSSA Dataset
aus2012 <- readRDS(url("https://mqsociology.github.io/learn-r/soci832/aussa2012.RDS"))
# Example 3: Australian Electoral Survey
aes_full <- readRDS(gzcon(url("https://mqsociology.github.io/learn-r/soci832/aes_full.rds")))
# Codebook
browseURL("https://mqsociology.github.io/learn-r/soci832/aes_full_codebook.html")
Testing reliability of the factors using Cronbach’s Alpha
Example 1: Crimes
Calculate Cronbach’s Alpha for Factor 1 (PA1)
crimes %>%
select(mottheft, arson, steafrmot, brkentdwel,
damtoprpty, steafrdwel, robbery, trespass,
weapon, offlang, sexoff, brchavo, astdomviol,
hrssthreat, brchbailcon, rsthindofficer, astnondomviol) %>%
as.matrix() %>%
psych::alpha()
##
## Reliability analysis
## Call: psych::alpha(x = .)
##
## raw_alpha std.alpha G6(smc) average_r S/N ase mean sd median_r
## 0.94 0.97 0.98 0.66 33 0.0042 328 202 0.66
##
## lower alpha upper 95% confidence boundaries
## 0.93 0.94 0.95
##
## Reliability if an item is dropped:
## raw_alpha std.alpha G6(smc) average_r S/N alpha se var.r
## mottheft 0.94 0.97 0.98 0.66 31 0.0044 0.013
## arson 0.93 0.97 0.98 0.66 31 0.0044 0.014
## steafrmot 0.93 0.97 0.98 0.67 32 0.0044 0.012
## brkentdwel 0.93 0.97 0.98 0.65 30 0.0048 0.013
## damtoprpty 0.93 0.97 0.98 0.64 29 0.0055 0.012
## steafrdwel 0.93 0.97 0.98 0.65 30 0.0045 0.013
## robbery 0.94 0.97 0.98 0.67 33 0.0042 0.012
## trespass 0.93 0.97 0.98 0.66 31 0.0044 0.014
## weapon 0.94 0.97 0.98 0.67 32 0.0043 0.013
## offlang 0.94 0.97 0.98 0.66 32 0.0043 0.013
## sexoff 0.94 0.97 0.98 0.66 32 0.0043 0.013
## brchavo 0.93 0.97 0.98 0.65 30 0.0046 0.012
## astdomviol 0.93 0.97 0.98 0.65 29 0.0048 0.012
## hrssthreat 0.93 0.97 0.98 0.65 30 0.0047 0.012
## brchbailcon 0.94 0.97 0.98 0.67 32 0.0035 0.013
## rsthindofficer 0.94 0.97 0.98 0.66 31 0.0043 0.013
## astnondomviol 0.93 0.97 0.98 0.65 30 0.0046 0.013
## med.r
## mottheft 0.66
## arson 0.66
## steafrmot 0.66
## brkentdwel 0.66
## damtoprpty 0.65
## steafrdwel 0.66
## robbery 0.67
## trespass 0.66
## weapon 0.67
## offlang 0.66
## sexoff 0.66
## brchavo 0.66
## astdomviol 0.65
## hrssthreat 0.66
## brchbailcon 0.66
## rsthindofficer 0.66
## astnondomviol 0.66
##
## Item statistics
## n raw.r std.r r.cor r.drop mean sd
## mottheft 120 0.79 0.80 0.79 0.78 179 116
## arson 119 0.80 0.83 0.83 0.80 119 136
## steafrmot 120 0.77 0.76 0.75 0.75 490 303
## brkentdwel 120 0.89 0.89 0.89 0.88 458 406
## damtoprpty 120 0.96 0.95 0.96 0.94 955 611
## steafrdwel 120 0.85 0.86 0.85 0.83 316 167
## robbery 90 0.69 0.70 0.69 0.66 28 28
## trespass 120 0.81 0.82 0.81 0.78 211 195
## weapon 120 0.70 0.73 0.71 0.67 234 148
## offlang 116 0.73 0.78 0.76 0.73 64 59
## sexoff 120 0.72 0.77 0.75 0.71 208 102
## brchavo 120 0.90 0.87 0.87 0.87 260 213
## astdomviol 120 0.94 0.92 0.93 0.92 437 295
## hrssthreat 120 0.91 0.88 0.88 0.88 485 327
## brchbailcon 120 0.78 0.74 0.72 0.71 512 519
## rsthindofficer 117 0.79 0.81 0.80 0.78 99 82
## astnondomviol 120 0.88 0.88 0.88 0.86 441 256
Calculate Cronbach’s Alpha for Factor 2 (PA3)
crimes %>%
select(liqoff, offcond, marijuana, oththeft, rsthindofficer) %>%
as.matrix() %>%
psych::alpha()
##
## Reliability analysis
## Call: psych::alpha(x = .)
##
## raw_alpha std.alpha G6(smc) average_r S/N ase mean sd median_r
## 0.82 0.89 0.88 0.63 8.5 0.019 213 135 0.64
##
## lower alpha upper 95% confidence boundaries
## 0.78 0.82 0.86
##
## Reliability if an item is dropped:
## raw_alpha std.alpha G6(smc) average_r S/N alpha se var.r
## liqoff 0.78 0.89 0.86 0.67 8.1 0.020 0.0019
## offcond 0.78 0.85 0.82 0.59 5.8 0.024 0.0045
## marijuana 0.82 0.88 0.85 0.64 7.0 0.020 0.0035
## oththeft 0.74 0.87 0.84 0.62 6.6 0.027 0.0083
## rsthindofficer 0.80 0.87 0.85 0.63 6.8 0.023 0.0067
## med.r
## liqoff 0.68
## offcond 0.61
## marijuana 0.63
## oththeft 0.64
## rsthindofficer 0.64
##
## Item statistics
## n raw.r std.r r.cor r.drop mean sd
## liqoff 119 0.75 0.78 0.70 0.63 168 147
## offcond 118 0.86 0.89 0.87 0.81 93 90
## marijuana 120 0.89 0.83 0.78 0.71 351 292
## oththeft 120 0.86 0.85 0.80 0.75 352 196
## rsthindofficer 117 0.78 0.84 0.78 0.73 99 82
Calculate Cronbach’s Alpha for Factor 3 (PA2)
crimes %>%
select(fraud, steafrprsn, steafrsto, transport,
recvstlgoods) %>%
as.matrix() %>%
psych::alpha()
##
## Reliability analysis
## Call: psych::alpha(x = .)
##
## raw_alpha std.alpha G6(smc) average_r S/N ase mean sd median_r
## 0.38 0.89 0.89 0.61 7.8 0.036 298 300 0.66
##
## lower alpha upper 95% confidence boundaries
## 0.31 0.38 0.45
##
## Reliability if an item is dropped:
## raw_alpha std.alpha G6(smc) average_r S/N alpha se var.r
## fraud 0.22 0.84 0.83 0.57 5.4 0.034 0.0326
## steafrprsn 0.37 0.84 0.84 0.57 5.3 0.037 0.0272
## steafrsto 0.31 0.86 0.84 0.61 6.2 0.028 0.0174
## transport 0.77 0.91 0.89 0.71 9.9 0.018 0.0028
## recvstlgoods 0.37 0.85 0.84 0.59 5.6 0.037 0.0256
## med.r
## fraud 0.59
## steafrprsn 0.61
## steafrsto 0.61
## transport 0.70
## recvstlgoods 0.62
##
## Item statistics
## n raw.r std.r r.cor r.drop mean sd
## fraud 120 0.75 0.88 0.86 0.66 467 246
## steafrprsn 101 0.63 0.89 0.86 0.64 43 57
## steafrsto 118 0.58 0.83 0.80 0.44 250 215
## transport 83 0.96 0.68 0.56 0.51 847 1370
## recvstlgoods 115 0.59 0.86 0.84 0.58 79 61
This shows that we should remove ‘transport’ from scale.