R Markdown

# Reading into the data, finding the dimensions, and finding which variables are discrete and which variables are continuous 

spm = read.csv('/Users/aaron/Downloads/spm2022_data.csv')
dim(spm) # 21 variables, 471 observations
## [1] 471  21
summary(spm)
##        wt               Age             Mar            Sex       
##  Min.   :  10.00   Min.   :20.00   Min.   :1.00   Min.   :1.000  
##  1st Qu.:  53.00   1st Qu.:40.50   1st Qu.:1.00   1st Qu.:1.000  
##  Median :  77.00   Median :56.00   Median :1.00   Median :2.000  
##  Mean   :  99.94   Mean   :54.24   Mean   :2.27   Mean   :1.505  
##  3rd Qu.: 118.50   3rd Qu.:67.00   3rd Qu.:3.00   3rd Qu.:2.000  
##  Max.   :1136.00   Max.   :94.00   Max.   :5.00   Max.   :2.000  
##    Education          Race            AGI            HI_premium   
##  Min.   :0.000   Min.   :1.000   Min.   :      0   Min.   :    0  
##  1st Qu.:2.000   1st Qu.:1.000   1st Qu.:  27648   1st Qu.:    0  
##  Median :3.000   Median :1.000   Median :  65000   Median :  576  
##  Mean   :2.958   Mean   :1.633   Mean   :  94878   Mean   : 1667  
##  3rd Qu.:4.000   3rd Qu.:2.000   3rd Qu.: 115552   3rd Qu.: 2000  
##  Max.   :4.000   Max.   :4.000   Max.   :1568000   Max.   :24000  
##    Moop_other      SPM_PovThreshold   SPM_NumPer     SPM_NumKids    
##  Min.   :    0.0   Min.   :11420    Min.   : 1.00   Min.   :0.0000  
##  1st Qu.:  156.5   1st Qu.:16913    1st Qu.: 1.00   1st Qu.:0.0000  
##  Median :  540.0   Median :21516    Median : 2.00   Median :0.0000  
##  Mean   : 1170.6   Mean   :25050    Mean   : 2.48   Mean   :0.5563  
##  3rd Qu.: 1297.0   3rd Qu.:31361    3rd Qu.: 3.00   3rd Qu.:1.0000  
##  Max.   :15100.0   Max.   :86732    Max.   :11.00   Max.   :7.0000  
##  SPM_NumAdults   SPM_TenMortStatus   SPM_Totval        SPM_FedTax    
##  Min.   :1.000   Min.   :1.000     Min.   :      0   Min.   : -9757  
##  1st Qu.:1.000   1st Qu.:1.000     1st Qu.:  42500   1st Qu.:     0  
##  Median :2.000   Median :2.000     Median :  81000   Median :  4305  
##  Mean   :1.924   Mean   :1.822     Mean   : 115727   Mean   : 13463  
##  3rd Qu.:2.000   3rd Qu.:2.500     3rd Qu.: 133150   3rd Qu.: 12252  
##  Max.   :6.000   Max.   :3.000     Max.   :1568000   Max.   :501798  
##   SPM_FedTaxBC       SPM_EITC         SPM_FICA         SPM_StTax    
##  Min.   :     0   Min.   :   0.0   Min.   :    0.0   Min.   :-2175  
##  1st Qu.:  1283   1st Qu.:   0.0   1st Qu.:  573.5   1st Qu.:    0  
##  Median :  5697   Median :   0.0   Median : 4590.0   Median : 1070  
##  Mean   : 14864   Mean   : 292.5   Mean   : 6125.0   Mean   : 3291  
##  3rd Qu.: 13843   3rd Qu.:   0.0   3rd Qu.: 8989.0   3rd Qu.: 3657  
##  Max.   :499986   Max.   :6935.0   Max.   :50348.0   Max.   :77744  
##  SPM_CapWkCCXpns
##  Min.   :    0  
##  1st Qu.:  620  
##  Median : 1612  
##  Mean   : 2126  
##  3rd Qu.: 3225  
##  Max.   :24825
# variable name -> definition

# wt -> weight
# mar -> marriage
# AGI -> adjusted gross income
# HI_premium -> Health insurance premium
# Moop_other -> Total medical out of pocket expenditures 
# SPM_PovThreshold -> SPM unit's SPM poverty threshold x?
# SPM_NumPer -> SPM unit's number of persons in household x
# SPM_TenMortStatus -> SPM unit's tenure/mortgage status x
# SPM_Totval -> SPM unit's cash income
# SPM_FedTaxBC -> SPM unit's Federal tax before refundable tax credits
# SPM_EITC -> SPM unit's Federal Earned Income Tax Credit
# SPM_FICA -> SPM unit's Federal Insurance Contributions Act and federal retirement contribution
# SPM_StTax -> SPM unit's state tax
# SPM_CapWkCCXpns -> SPM unit's capped work and child care expenses
# Calculating the correlation our two chosen variables, AGI and SPM_FICA
cor(spm$AGI, spm$SPM_FICA) 
## [1] 0.7113186
# plotting AGI vs. SPM_FICA, with AGI as the explanatory variable to visually confirm a linear trend
plot(spm$AGI, spm$SPM_FICA,
     main = 'AGI vs. FICA',
     xlab = 'AGI',
     ylab = 'FICA')

# Plotting and calculating summary statistics and standard deviation for AGI
hist(spm$AGI)

summary(spm$AGI)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       0   27648   65000   94878  115552 1568000
sd(spm$AGI)
## [1] 133947.7
# Plotting and calculating summary statistics and standard deviation for SPM_FICA

hist(spm$SPM_FICA)

summary(spm$SPM_FICA)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0   573.5  4590.0  6125.0  8989.0 50348.0
sd(spm$SPM_FICA) 
## [1] 6643.309
# Fitting a linear model to our variables with AGI as the explanatory variable
spmod = lm(spm$SPM_FICA ~ spm$AGI)

# Summarizing the different aspects of the linear model to find the slope and intercept of the model
summary(spmod) 
## 
## Call:
## lm(formula = spm$SPM_FICA ~ spm$AGI)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -22351.7  -2806.1   -471.6   2120.7  29188.4 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.778e+03  2.640e+02   10.52   <2e-16 ***
## spm$AGI     3.528e-02  1.610e-03   21.92   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4674 on 469 degrees of freedom
## Multiple R-squared:  0.506,  Adjusted R-squared:  0.5049 
## F-statistic: 480.3 on 1 and 469 DF,  p-value: < 2.2e-16
# Residual plot to look for any clear patterns
plot(spmod$residuals ~ spm$AGI,
     main = 'Residual Plot',
     xlab = 'AGI',
     ylab = 'Residuals')
     abline(h = 0, 
            col = 'red', 
            lwd = 1.5)