library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(dplyr)
library(readxl)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library_patrons <- read_excel("library_patrons.xls")
patrons_model1 <- lm(reg ~ zip + total + Percpop + inc, data = library_patrons)
plot(patrons_model1,which = 1)
This model is linear by visual inspection
library (car)
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
##
## recode
## The following object is masked from 'package:purrr':
##
## some
durbinWatsonTest(patrons_model1)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.0784514 2.146319 0.596
## Alternative hypothesis: rho != 0
The model errors no p value given = NA
plot(patrons_model1, which = 3)
bptest(patrons_model1)
##
## studentized Breusch-Pagan test
##
## data: patrons_model1
## BP = 26.1, df = 4, p-value = 3.022e-05
Model is homoskedatistic per bp_test (value over.05)
plot(patrons_model1,which = 2)
Model is normal by visual inspection
vif(patrons_model1)
## zip total Percpop inc
## 1.199642 1.143033 1.191669 1.128111
The assumptions not met are linearity and independence of errors
patrons_model1_log <- log(hp) ~ zip + total + percpop + inc
model was log transformed to help with linearity but final result is still not linear
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
patrons_model_rlm <- rlm(reg ~ zip + total + Percpop + inc, data = library_patrons)
durbinWatsonTest(patrons_model_rlm)
## lag Autocorrelation D-W Statistic p-value
## 1 -0.06322017 2.123888 0.664
## Alternative hypothesis: rho != 0