data <- read_csv("/Users/joe/Documents/CUNY_SPS/DATA_605_Fundamentals_of_Computational_Math/Homework_DATA_605/anemia.csv")
## Rows: 1421 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (6): Gender, Hemoglobin, MCH, MCHC, MCV, Result
##
## ℹ 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.
summary(data)
## Gender Hemoglobin MCH MCHC
## Min. :0.0000 Min. : 6.60 Min. :16.00 Min. :27.80
## 1st Qu.:0.0000 1st Qu.:11.70 1st Qu.:19.40 1st Qu.:29.00
## Median :1.0000 Median :13.20 Median :22.70 Median :30.40
## Mean :0.5208 Mean :13.41 Mean :22.91 Mean :30.25
## 3rd Qu.:1.0000 3rd Qu.:15.00 3rd Qu.:26.20 3rd Qu.:31.40
## Max. :1.0000 Max. :16.90 Max. :30.00 Max. :32.50
## MCV Result
## Min. : 69.40 Min. :0.0000
## 1st Qu.: 77.30 1st Qu.:0.0000
## Median : 85.30 Median :0.0000
## Mean : 85.52 Mean :0.4363
## 3rd Qu.: 94.20 3rd Qu.:1.0000
## Max. :101.60 Max. :1.0000
I went ahead and renamed a few columns, now we know what the abbreviations are.
names(data)[names(data) == 'MCH'] <- 'Mean_Corpuscular_Hemoglobin'
names(data)[names(data) == 'MCHC'] <- 'Mean_Corpuscular_Hemoglobin_Concentration'
names(data)[names(data) == 'MCV'] <- 'Mean_Corpuscular_Volumn'
names(data)[names(data) == 'Result'] <- 'Result_For_Anemia'
data
## # A tibble: 1,421 × 6
## Gender Hemoglobin Mean_Corpuscular_Hemoglobin Mean_Corpuscular_Hemoglobin_C…¹
## <dbl> <dbl> <dbl> <dbl>
## 1 1 14.9 22.7 29.1
## 2 0 15.9 25.4 28.3
## 3 0 9 21.5 29.6
## 4 0 14.9 16 31.4
## 5 1 14.7 22 28.2
## 6 0 11.6 22.3 30.9
## 7 1 12.7 19.5 28.9
## 8 1 12.7 28.5 28.2
## 9 0 14.1 29.7 30.5
## 10 1 14.9 25.8 31.3
## # ℹ 1,411 more rows
## # ℹ abbreviated name: ¹Mean_Corpuscular_Hemoglobin_Concentration
## # ℹ 2 more variables: Mean_Corpuscular_Volumn <dbl>, Result_For_Anemia <dbl>
We have added 2 columns into our data. One is the Mean_Corpuscular_Hemoglobin squared and the other is Gender multiplied with Mean_Corpuscular_Hemoglobin_Concentration.
The first is to get a Quadratic Term.
We also need to get a Dischotomous Term, which this dataset has provided with in the Result_For_Anemia and Gender.
Finally, we need One Dichotomous vs Quantitative Interaction Term. We can take the Gender * Mean_Corpuscular_Hemoglobin_Concentration them together.
data1 <- data %>%
mutate( Mean_Corpuscular_Hemoglobin_squared = `Mean_Corpuscular_Hemoglobin`^2,
hemoglobin2= `Gender` * `Mean_Corpuscular_Hemoglobin_Concentration`)
data1
## # A tibble: 1,421 × 8
## Gender Hemoglobin Mean_Corpuscular_Hemoglobin Mean_Corpuscular_Hemoglobin_C…¹
## <dbl> <dbl> <dbl> <dbl>
## 1 1 14.9 22.7 29.1
## 2 0 15.9 25.4 28.3
## 3 0 9 21.5 29.6
## 4 0 14.9 16 31.4
## 5 1 14.7 22 28.2
## 6 0 11.6 22.3 30.9
## 7 1 12.7 19.5 28.9
## 8 1 12.7 28.5 28.2
## 9 0 14.1 29.7 30.5
## 10 1 14.9 25.8 31.3
## # ℹ 1,411 more rows
## # ℹ abbreviated name: ¹Mean_Corpuscular_Hemoglobin_Concentration
## # ℹ 4 more variables: Mean_Corpuscular_Volumn <dbl>, Result_For_Anemia <dbl>,
## # Mean_Corpuscular_Hemoglobin_squared <dbl>, hemoglobin2 <dbl>
Using the pairs() function, we get a visual sense of the relationships within the data.
pairs(data1, gap = 0.5)
The function call gives us a linear model object, we labeled it “.full” because it gives all the possibilites the our data set. We use “.lm” to avoid confusion when working with other models. The residuals balance is around 0 with -0.1868, which means our models are good.
model.lm.full <- lm(`Hemoglobin`~ Gender + Mean_Corpuscular_Hemoglobin + Mean_Corpuscular_Hemoglobin_Concentration + Mean_Corpuscular_Volumn+ hemoglobin2 + Mean_Corpuscular_Hemoglobin_squared, data = data1)
summary(model.lm.full)
##
## Call:
## lm(formula = Hemoglobin ~ Gender + Mean_Corpuscular_Hemoglobin +
## Mean_Corpuscular_Hemoglobin_Concentration + Mean_Corpuscular_Volumn +
## hemoglobin2 + Mean_Corpuscular_Hemoglobin_squared, data = data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.6175 -1.5978 -0.1868 1.6693 3.7979
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.372950 2.687421 3.116 0.001873
## Gender -0.201775 2.269897 -0.089 0.929180
## Mean_Corpuscular_Hemoglobin 0.612044 0.170460 3.591 0.000341
## Mean_Corpuscular_Hemoglobin_Concentration -0.047133 0.055375 -0.851 0.394823
## Mean_Corpuscular_Volumn -0.005229 0.005447 -0.960 0.337254
## hemoglobin2 0.006740 0.074990 0.090 0.928397
## Mean_Corpuscular_Hemoglobin_squared -0.013152 0.003696 -3.558 0.000385
##
## (Intercept) **
## Gender
## Mean_Corpuscular_Hemoglobin ***
## Mean_Corpuscular_Hemoglobin_Concentration
## Mean_Corpuscular_Volumn
## hemoglobin2
## Mean_Corpuscular_Hemoglobin_squared ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.967 on 1414 degrees of freedom
## Multiple R-squared: 0.01158, Adjusted R-squared: 0.007391
## F-statistic: 2.762 on 6 and 1414 DF, p-value: 0.01131
We are doing an example of Backward Elimination. We take the predictor with the largest p-value which in our case is Gender. We use the update() function to recompute the model.
model.lm.2 <- update(model.lm.full, .~. - Gender, data = data1)
summary(model.lm.2)
##
## Call:
## lm(formula = Hemoglobin ~ Mean_Corpuscular_Hemoglobin + Mean_Corpuscular_Hemoglobin_Concentration +
## Mean_Corpuscular_Volumn + hemoglobin2 + Mean_Corpuscular_Hemoglobin_squared,
## data = data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.6197 -1.5988 -0.1804 1.6665 3.7990
##
## Coefficients:
## Estimate Std. Error t value
## (Intercept) 8.257e+00 2.349e+00 3.515
## Mean_Corpuscular_Hemoglobin 6.129e-01 1.702e-01 3.602
## Mean_Corpuscular_Hemoglobin_Concentration -4.353e-02 3.769e-02 -1.155
## Mean_Corpuscular_Volumn -5.254e-03 5.437e-03 -0.966
## hemoglobin2 8.107e-05 3.473e-03 0.023
## Mean_Corpuscular_Hemoglobin_squared -1.317e-02 3.689e-03 -3.570
## Pr(>|t|)
## (Intercept) 0.000453 ***
## Mean_Corpuscular_Hemoglobin 0.000327 ***
## Mean_Corpuscular_Hemoglobin_Concentration 0.248330
## Mean_Corpuscular_Volumn 0.334102
## hemoglobin2 0.981380
## Mean_Corpuscular_Hemoglobin_squared 0.000369 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.967 on 1415 degrees of freedom
## Multiple R-squared: 0.01158, Adjusted R-squared: 0.008087
## F-statistic: 3.315 on 5 and 1415 DF, p-value: 0.005558
We next move onto the predictor that has the next largest p-value, hemoglobin2
model.lm.3 <- update(model.lm.2, .~. - hemoglobin2, data = data1)
summary(model.lm.3)
##
## Call:
## lm(formula = Hemoglobin ~ Mean_Corpuscular_Hemoglobin + Mean_Corpuscular_Hemoglobin_Concentration +
## Mean_Corpuscular_Volumn + Mean_Corpuscular_Hemoglobin_squared,
## data = data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.6209 -1.6000 -0.1816 1.6678 3.7979
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.251897 2.337865 3.530 0.000429
## Mean_Corpuscular_Hemoglobin 0.613297 0.169077 3.627 0.000297
## Mean_Corpuscular_Hemoglobin_Concentration -0.043459 0.037559 -1.157 0.247439
## Mean_Corpuscular_Volumn -0.005260 0.005429 -0.969 0.332834
## Mean_Corpuscular_Hemoglobin_squared -0.013180 0.003666 -3.596 0.000335
##
## (Intercept) ***
## Mean_Corpuscular_Hemoglobin ***
## Mean_Corpuscular_Hemoglobin_Concentration
## Mean_Corpuscular_Volumn
## Mean_Corpuscular_Hemoglobin_squared ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.966 on 1416 degrees of freedom
## Multiple R-squared: 0.01158, Adjusted R-squared: 0.008787
## F-statistic: 4.147 on 4 and 1416 DF, p-value: 0.002414
model.lm.4 <- update(model.lm.3, .~. - Mean_Corpuscular_Volumn, data = data1)
summary(model.lm.4)
##
## Call:
## lm(formula = Hemoglobin ~ Mean_Corpuscular_Hemoglobin + Mean_Corpuscular_Hemoglobin_Concentration +
## Mean_Corpuscular_Hemoglobin_squared, data = data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.6014 -1.6321 -0.1821 1.6762 3.8336
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.930196 2.314110 3.427 0.000628
## Mean_Corpuscular_Hemoglobin 0.608642 0.169005 3.601 0.000328
## Mean_Corpuscular_Hemoglobin_Concentration -0.046064 0.037462 -1.230 0.219049
## Mean_Corpuscular_Hemoglobin_squared -0.013074 0.003664 -3.568 0.000371
##
## (Intercept) ***
## Mean_Corpuscular_Hemoglobin ***
## Mean_Corpuscular_Hemoglobin_Concentration
## Mean_Corpuscular_Hemoglobin_squared ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.966 on 1417 degrees of freedom
## Multiple R-squared: 0.01092, Adjusted R-squared: 0.00883
## F-statistic: 5.217 on 3 and 1417 DF, p-value: 0.001389
model.lm.5 <- update(model.lm.4, .~. - Mean_Corpuscular_Hemoglobin_Concentration, data = data1)
summary(model.lm.5)
##
## Call:
## lm(formula = Hemoglobin ~ Mean_Corpuscular_Hemoglobin + Mean_Corpuscular_Hemoglobin_squared,
## data = data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.6260 -1.6331 -0.1861 1.6678 3.8548
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.295200 1.894286 3.323 0.000912 ***
## Mean_Corpuscular_Hemoglobin 0.630613 0.168088 3.752 0.000183 ***
## Mean_Corpuscular_Hemoglobin_squared -0.013558 0.003643 -3.721 0.000206 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.966 on 1418 degrees of freedom
## Multiple R-squared: 0.009868, Adjusted R-squared: 0.008472
## F-statistic: 7.066 on 2 and 1418 DF, p-value: 0.0008837
The Coefficients stand for the following: Intercept (6.295200) -Does not have a direct interpretation to our model. Mean_Corpuscular_Hemoglobin MCH (0.630613) - It means that the variable (Hemoglobin) will increase 0.630613 units in response to Mean_Corpuscular_Hemoglobin increasing by 1. Mean_Corpuscular_Hemoglobin_squared (-0.013558) - As Mean_Corpuscilar_Hemoglobin MCH increases the variable decreases, thus a negative number.
plot(data1$Mean_Corpuscular_Hemoglobin, data1$Hemoglobin,
main="Scatter plot with Linear Regression Line",
xlab="Mean_Corpuscular_Hemoglobin", ylab="Response Variable")
lm_model <- lm(Hemoglobin ~ Mean_Corpuscular_Hemoglobin, data=data1)
abline(lm_model, col="red")
There is no linear regression at all.
par(mmfrow = c(2.2))
## Warning in par(mmfrow = c(2.2)): "mmfrow" is not a graphical parameter
plot(model.lm.5)
The Residuals vs Fitted looks normal with a straight line across the 0 axis. The model does not seem to be appropriate, as the Q-Q Residual is forming an S like state around the line.The Residual vs Fitted showed the data points clump around 13.5. The Residual vs Leverage seems to draw a line between 0.000 - 0.005.