library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── 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(readxl)
library(pastecs)
## 
## Attaching package: 'pastecs'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
setwd("C:/Users/David/Desktop/My Class Stuff/Wednesday Class")
data_florida <- read_excel("Data_Florida.xlsx", sheet = "Award Year Summary")
## New names:
## • `Recipients` -> `Recipients...6`
## • `# of Loans Originated` -> `# of Loans Originated...7`
## • `$ of Loans Originated` -> `$ of Loans Originated...8`
## • `# of Disbursements` -> `# of Disbursements...9`
## • `$ of Disbursements` -> `$ of Disbursements...10`
## • `Recipients` -> `Recipients...11`
## • `# of Loans Originated` -> `# of Loans Originated...12`
## • `$ of Loans Originated` -> `$ of Loans Originated...13`
## • `# of Disbursements` -> `# of Disbursements...14`
## • `$ of Disbursements` -> `$ of Disbursements...15`
## • `Recipients` -> `Recipients...16`
## • `# of Loans Originated` -> `# of Loans Originated...17`
## • `$ of Loans Originated` -> `$ of Loans Originated...18`
## • `# of Disbursements` -> `# of Disbursements...19`
## • `$ of Disbursements` -> `$ of Disbursements...20`
## • `Recipients` -> `Recipients...21`
## • `# of Loans Originated` -> `# of Loans Originated...22`
## • `$ of Loans Originated` -> `$ of Loans Originated...23`
## • `# of Disbursements` -> `# of Disbursements...24`
## • `$ of Disbursements` -> `$ of Disbursements...25`
recipients <- as.numeric(data_florida$`Recipients...6`)
loans_count <- as.numeric(data_florida$`# of Loans Originated...7`)
loans_dollars <- as.numeric(data_florida$`$ of Loans Originated...8`)
fl_lm <- data_florida %>%
  dplyr::select(
    total_loans = `$ of Loans Originated...8`,
    recipients   = `Recipients...6`,
    school_type  = `School Type`
  ) %>%
  dplyr::mutate(
    total_loans = as.numeric(total_loans),
    recipients  = as.numeric(recipients),
    school_type = as.factor(school_type))
head(fl_lm)
## # A tibble: 6 × 3
##   total_loans recipients school_type
##         <dbl>      <dbl> <fct>      
## 1     1546994        291 PRIVATE    
## 2     6394735       1413 PUBLIC     
## 3     1866473        406 PUBLIC     
## 4    12780036       2998 PUBLIC     
## 5      103869         38 PROPRIETARY
## 6      516838        192 PROPRIETARY
model1 <- lm(total_loans ~ recipients + school_type, data = fl_lm)
summary(model1)
## 
## Call:
## lm(formula = total_loans ~ recipients + school_type, data = fl_lm)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -59988642   -510703    -55763    183084  44901315 
## 
## Coefficients:
##                              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                 2.665e+06  1.041e+06   2.561  0.01048 *  
## recipients                  4.519e+03  1.698e+01 266.133  < 2e-16 ***
## school_typeFOREIGN PRIVATE -2.564e+06  1.070e+06  -2.398  0.01654 *  
## school_typeFOREIGN PUBLIC  -2.601e+06  1.047e+06  -2.483  0.01306 *  
## school_typeOTHER           -2.666e+06  2.327e+06  -1.146  0.25190    
## school_typePRIVATE         -2.157e+06  1.042e+06  -2.070  0.03855 *  
## school_typePROPRIETARY     -2.868e+06  1.043e+06  -2.750  0.00598 ** 
## school_typePUBLIC          -2.871e+06  1.042e+06  -2.754  0.00591 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2081000 on 3812 degrees of freedom
## Multiple R-squared:  0.9508, Adjusted R-squared:  0.9507 
## F-statistic: 1.051e+04 on 7 and 3812 DF,  p-value: < 2.2e-16
plot(model1, which = 1)

library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
raintest(model1)
## 
##  Rainbow test
## 
## data:  model1
## Rain = 0.45669, df1 = 1910, df2 = 1902, p-value = 1
lmtest::dwtest(model1)
## 
##  Durbin-Watson test
## 
## data:  model1
## DW = 1.9077, p-value = 0.00194
## alternative hypothesis: true autocorrelation is greater than 0
lmtest::bptest(model1)
## 
##  studentized Breusch-Pagan test
## 
## data:  model1
## BP = 1208.7, df = 7, p-value < 2.2e-16
plot(model1, which=2)
## Warning: not plotting observations with leverage one:
##   2391

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
vif(model1)
##                 GVIF Df GVIF^(1/(2*Df))
## recipients  1.040655  1        1.020125
## school_type 1.040655  6        1.003326

#The model meets most assumptins. The residuals looks reasonably linear. The variance is constant. VIFs are low