#Loading Data
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(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
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
hotline_data <- read_csv("hotline_data.csv")
## Rows: 2184 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): State, Timeframe, Call Average Time to Answer, Text Average Time to...
## dbl (6): Number of Routed Calls, Call Answer Rate, Number of Routed Texts, T...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Creating Texas Subset
texas_data <- hotline_data %>% filter(State == "TX")
#Creating Time Variable
texas_data <- texas_data %>% mutate(Time = row_number())
#Running Basic Descriptive Statistics: Summary
summary(texas_data[, c("Call Answer Rate", "Number of Routed Calls", "Time")])
## Call Answer Rate Number of Routed Calls Time
## Min. :0.5617 Min. :13787 Min. : 1.0
## 1st Qu.:0.7623 1st Qu.:14886 1st Qu.:10.5
## Median :0.8348 Median :16022 Median :20.0
## Mean :0.7918 Mean :17377 Mean :20.0
## 3rd Qu.:0.8526 3rd Qu.:19294 3rd Qu.:29.5
## Max. :0.8709 Max. :24509 Max. :39.0
#Running Basic Descriptive Statistics:Answer Rate Over Time
plot(texas_data$Time,
texas_data$`Call Answer Rate`,
xlab = "Time",
ylab = "Call Answer Rate",
main = "Texas 988 Call Answer Rate Over Time")
#Running Basic Descriptive Statistics: Answer Rate vs. Call Volume
plot(texas_data$`Number of Routed Calls`,
texas_data$`Call Answer Rate`,
xlab = "Number of Routed Calls",
ylab = "Call Answer Rate",
main = "Texas 988 Call Volume and Answer Rate")
#Creating a Linear Model
texas_model <- lm(`Call Answer Rate`~`Number of Routed Calls`+ Time,data=texas_data)
summary(texas_model)
##
## Call:
## lm(formula = `Call Answer Rate` ~ `Number of Routed Calls` +
## Time, data = texas_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.09730 -0.02269 0.00756 0.02446 0.09029
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.127e+00 7.259e-02 15.528 < 2e-16 ***
## `Number of Routed Calls` -3.457e-05 5.629e-06 -6.142 4.50e-07 ***
## Time 1.326e-02 1.514e-03 8.763 1.87e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.04839 on 36 degrees of freedom
## Multiple R-squared: 0.7147, Adjusted R-squared: 0.6988
## F-statistic: 45.08 on 2 and 36 DF, p-value: 1.571e-10
#Testing Linearity
plot(texas_model, which=1)
Based on visual inspection, linearity is questionable and appears to be
violated.
#Testing Independence of Errors
dwtest(texas_model)
##
## Durbin-Watson test
##
## data: texas_model
## DW = 0.62932, p-value = 3.724e-08
## alternative hypothesis: true autocorrelation is greater than 0
The model’s errors are not independent per the Durbin Watson Test (p-value < 0.05) and appears to be violated.
#Testing Homoscedasticity
plot(texas_model,which=3)
bptest(texas_model)
##
## studentized Breusch-Pagan test
##
## data: texas_model
## BP = 0.45293, df = 2, p-value = 0.7973
The model is homoscedastistic per the BP-test (p-value > 0.05) and is not violated.
#Testing Normality of Residuals
plot(texas_model,which = 2)
Model appears to be normally distributed based on visual inspection.
Some deviations from the reference line are present in the tails.
#Testing Multicolinarity
vif(texas_model)
## `Number of Routed Calls` Time
## 4.834381 4.834381
Both independent variables have no severe multicollinearity (VIF < 5).
#Creating a Mitigation Log
texas_model_log<-lm(log(`Call Answer Rate`)~log(`Number of Routed Calls`)+log(Time),data=texas_data)
#Mitigating Linearity
plot(texas_model_log,which = 1)
Model was log-transformed to help with linearity. Based on visual inspection, linearity appears to still not have been met. The assumption may still be violated.
# Copy the data and replace spaces in column names with periods
texas_clean <- texas_data
names(texas_clean) <- make.names(names(texas_clean), unique = TRUE)
# Fit the model using the cleaned names
texas_model_changed <- MASS::rlm(
Call.Answer.Rate ~ Number.of.Routed.Calls + Time,
data = texas_clean
)
summary(texas_model_changed)
##
## Call: rlm(formula = Call.Answer.Rate ~ Number.of.Routed.Calls + Time,
## data = texas_clean)
## Residuals:
## Min 1Q Median 3Q Max
## -0.099192 -0.022518 0.001704 0.021367 0.095144
##
## Coefficients:
## Value Std. Error t value
## (Intercept) 1.1614 0.0786 14.7734
## Number.of.Routed.Calls 0.0000 0.0000 -6.0205
## Time 0.0135 0.0016 8.2116
##
## Residual standard error: 0.03351 on 36 degrees of freedom
#Mitigation for Independence of Errors
dwtest(texas_model_changed)
##
## Durbin-Watson test
##
## data: texas_model_changed
## DW = 0.62932, p-value = 3.724e-08
## alternative hypothesis: true autocorrelation is greater than 0
The Durbin-Watson result did not show that the independence issue disappeared.