library(readxl)
library(dplyr)
library(DT)
setwd("C:/Users/ajgan/downloads")
Data_Health_Ass4 <- read_excel("Data_Health_Ass4.xlsx")
# Remove incomplete rows
Data_Health_Ass4_clean <- Data_Health_Ass4 %>%
na.omit()
# Interactive table
datatable(Data_Health_Ass4_clean,
options = list(pagelength = 10,
scrollX = TRUE))DS7310 Assignment 4
Assignment Guideline (20 Points)
Create a folder for this project and call it as DS7310_Assignment4_YourFullName.
Create a project file in the folder.
Change the name of author in YAML.
Save this file as you lastname_firstname_Assignment4.qmd
Add a table of content
Add your code to the R code chunkes.
Compress (zip) the DS7310_Assignment4_YourFullName folder.
After upload the compress folder, download it, and make sure it is the correct one.
Question 1 (50 points)
- Import the Data_Health_Ass4, remove incomplete rows, and create an interactive table for that.
- Create an interactive histogram for Stroke variable.
#create an interactive histogram
library(ggplot2)
ggplot(
data = Data_Health_Ass4_clean,
aes(x = Stroke))+
geom_histogram(fill = "red", color = "white", bins = 30) +
labs(title = "Histogram of Stroke Rates",
x = "Stroke Rate",
y = "Count")Get a subset of data for “Stroke”,“BloodPressure”,“Depression”, and “HighCholesterol,”
Make stroke prediction models based on different combinations of selecting 2 or 3 variables from BloodPressure, Depression, and HighCholesterol. Call each model from 1 to 4, like Model1, Model3, Model3, and Model4.
Make sure to use as.numeric for the variables, like as.numeric(Stroke), to convert the four variables to a numeric data, such as
Data_Health_Ass4_subset$Stroke <- as.numeric(Data_Health_Ass4_subset$Stroke)
#create subset: "Stroke, blood pressure, depression and high cholesterol
Data_Health_Ass4_subset <- Data_Health_Ass4_clean %>%
select(Stroke, BloodPressure, Depression, HighCholesterol)
#as.numeric for variables
Data_Health_Ass4_subset <- Data_Health_Ass4_subset %>%
mutate(
Stroke = as.numeric(Stroke),
BloodPressure = as.numeric(BloodPressure),
Depression = as.numeric(Depression),
HighCholesterol = as.numeric(HighCholesterol))
#model 1:
model1 <- lm(Stroke ~ BloodPressure + Depression, data = Data_Health_Ass4_subset)
summary(model1)
Call:
lm(formula = Stroke ~ BloodPressure + Depression, data = Data_Health_Ass4_subset)
Residuals:
Min 1Q Median 3Q Max
-1.4076 -0.3004 -0.0246 0.2566 3.2568
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.097974 0.083547 -13.142 <2e-16 ***
BloodPressure 0.143544 0.001607 89.342 <2e-16 ***
Depression -0.003436 0.002960 -1.161 0.246
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4498 on 2476 degrees of freedom
Multiple R-squared: 0.7696, Adjusted R-squared: 0.7694
F-statistic: 4135 on 2 and 2476 DF, p-value: < 2.2e-16
#model 2:
model2 <- lm(Stroke ~ BloodPressure + HighCholesterol, data = Data_Health_Ass4_subset)
summary(model2)
Call:
lm(formula = Stroke ~ BloodPressure + HighCholesterol, data = Data_Health_Ass4_subset)
Residuals:
Min 1Q Median 3Q Max
-1.3969 -0.2976 -0.0283 0.2517 3.2461
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.103410 0.107445 -10.269 <2e-16 ***
BloodPressure 0.144481 0.002450 58.982 <2e-16 ***
HighCholesterol -0.003030 0.004347 -0.697 0.486
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4499 on 2476 degrees of freedom
Multiple R-squared: 0.7695, Adjusted R-squared: 0.7693
F-statistic: 4133 on 2 and 2476 DF, p-value: < 2.2e-16
#model 3:
model3 <- lm(Stroke ~ Depression + HighCholesterol, data = Data_Health_Ass4_subset)
summary(model3)
Call:
lm(formula = Stroke ~ Depression + HighCholesterol, data = Data_Health_Ass4_subset)
Residuals:
Min 1Q Median 3Q Max
-2.0490 -0.4289 -0.0697 0.3168 3.7800
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -3.089804 0.177777 -17.380 < 2e-16 ***
Depression 0.011876 0.004575 2.596 0.00949 **
HighCholesterol 0.191201 0.004407 43.390 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.6968 on 2476 degrees of freedom
Multiple R-squared: 0.4471, Adjusted R-squared: 0.4467
F-statistic: 1001 on 2 and 2476 DF, p-value: < 2.2e-16
#model 4:
model4 <- lm(Stroke ~ BloodPressure + Depression + HighCholesterol, data = Data_Health_Ass4_subset)
summary(model4)
Call:
lm(formula = Stroke ~ BloodPressure + Depression + HighCholesterol,
data = Data_Health_Ass4_subset)
Residuals:
Min 1Q Median 3Q Max
-1.4133 -0.2995 -0.0251 0.2525 3.2495
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.043555 0.119934 -8.701 <2e-16 ***
BloodPressure 0.144722 0.002459 58.858 <2e-16 ***
Depression -0.003330 0.002965 -1.123 0.262
HighCholesterol -0.002754 0.004354 -0.633 0.527
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4499 on 2475 degrees of freedom
Multiple R-squared: 0.7696, Adjusted R-squared: 0.7693
F-statistic: 2756 on 3 and 2475 DF, p-value: < 2.2e-16
Report the evaluation results of the four combinations in a table, with three columns of of Model variables (e.g., BloodPressure + Depression), model number from 1 to 4, and adjusted RSquared.
Show the report_lm_results with knitr::kable.
First create the empty table using the following code. Add your code to the following code.
report_lm_results <- data.frame(
Variables = character(),
Model = integer(),
Adj_R_squared = numeric(),
stringsAsFactors = FALSE
)
#Example: report_lm_results[1,1] <- "BloodPressure + Depression"
# report_lm_results[1,2] <- 1
# report_lm_results[1,3] <- summary(Model1)$adj.r.squared
#fill in the results for each model
#model_1
report_lm_results[1,1] <- "BloodPressure + Pressure"
report_lm_results[1,2] <- 1
report_lm_results[1,3] <- summary(model1)$adj.r.squared
#model_2
report_lm_results[2,1] <- "BloodPressure + HighCholesterol"
report_lm_results[2,2] <- 2
report_lm_results[2,3] <- summary(model2)$adj.r.squared
#model_3
report_lm_results[3,1] <- "Depression + HighCholesterol"
report_lm_results[3,2] <- 3
report_lm_results[3,3] <- summary(model3)$adj.r.squared
#model_4
report_lm_results[4,1] <- "BloodPressure + Depression + HighCholesterol"
report_lm_results[4,2] <- 4
report_lm_results[4,3] <- summary(model4)$adj.r.squared
#results
knitr:: kable(report_lm_results)| Variables | Model | Adj_R_squared |
|---|---|---|
| BloodPressure + Pressure | 1 | 0.7693899 |
| BloodPressure + HighCholesterol | 2 | 0.7693097 |
| Depression + HighCholesterol | 3 | 0.4466909 |
| BloodPressure + Depression + HighCholesterol | 4 | 0.7693340 |
- Find the summary of the best model.
models <- list(
model1 = model1,
model2 = model2,
model3 = model3,
model4 = model4
)
adj_r2_values <- sapply(models, function(m) summary(m)$adj.r.squared)
adj_r2_values model1 model2 model3 model4
0.7693899 0.7693097 0.4466909 0.7693340
best_model <- names(which.max(adj_r2_values))
best_model[1] "model1"
summary(models[[best_model]])
Call:
lm(formula = Stroke ~ BloodPressure + Depression, data = Data_Health_Ass4_subset)
Residuals:
Min 1Q Median 3Q Max
-1.4076 -0.3004 -0.0246 0.2566 3.2568
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.097974 0.083547 -13.142 <2e-16 ***
BloodPressure 0.143544 0.001607 89.342 <2e-16 ***
Depression -0.003436 0.002960 -1.161 0.246
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4498 on 2476 degrees of freedom
Multiple R-squared: 0.7696, Adjusted R-squared: 0.7694
F-statistic: 4135 on 2 and 2476 DF, p-value: < 2.2e-16
- Write the regression formula for the best model in bold format.
Type the formula here:
Stroke ~ Blood Pressure + Depression + High Cholesterol
Stroke = \(\beta_0\) + \(\beta_1\)(BloodPressure) + \(\beta_2\)(Depression) + \(\beta_3\)(highCholesterol) + \(\epsilon\)
Question 2 (30 points)
Cluster the data using k-means.
Select the best estimate number of clusters other than min.nc and max.nc.
set.seed(123)
#cluster the data
data_kmeans <- Data_Health_Ass4_subset
#scale the data
scaled_data <- scale(data_kmeans)
#Try k = 2 to 5
set.seed(123)
wss <- sapply(2:5, function(k) {
kmeans(scaled_data, centers = k, nstart = 25)$tot.withinss})
best_k <- which.min(wss) + 1
best_k[1] 5
set.seed(123)
kmeans_results <- kmeans(scaled_data, centers = best_k, nstart = 25)
kmeans_resultsK-means clustering with 5 clusters of sizes 854, 417, 349, 437, 422
Cluster means:
Stroke BloodPressure Depression HighCholesterol
1 -0.07891134 -0.0120943 0.07622062 0.1274650
2 -1.02793486 -1.2096735 0.18460734 -1.4544704
3 1.50488683 1.4175571 -0.58091886 0.8902141
4 -0.76757364 -0.7793328 -1.18769930 -0.4591696
5 0.72574243 0.8545104 1.37367678 0.9185577
Clustering vector:
[1] 1 1 3 5 5 3 3 1 3 5 1 3 3 5 5 1 1 3 3 5 5 5 5 3 5 1 3 5 5 5 5 3 3 3 1 5 1
[38] 5 1 5 2 1 3 3 1 3 5 1 1 3 1 5 3 3 1 5 3 1 4 3 3 3 1 5 3 3 5 3 1 2 1 2 2 3
[75] 4 3 1 2 1 4 1 4 5 3 5 2 5 3 1 5 3 2 5 5 5 1 5 1 5 5 5 3 5 2 5 5 5 1 5 5 5
[112] 1 5 5 5 3 5 3 5 3 1 5 5 1 5 5 5 5 3 3 5 3 5 3 5 5 5 5 5 1 5 3 1 5 5 1 5 5
[149] 1 5 2 5 3 5 4 1 2 1 4 4 1 4 2 1 1 4 4 4 2 1 2 4 2 4 3 1 2 2 4 4 1 4 4 1 4
[186] 2 2 2 2 4 4 2 4 2 4 2 1 1 4 4 2 4 1 3 4 1 4 2 2 2 2 2 4 2 2 4 2 4 3 2 1 2
[223] 2 2 2 2 4 2 2 2 2 1 2 4 2 2 2 1 2 2 2 2 1 4 2 1 4 2 2 4 4 2 1 2 2 4 2 2 4
[260] 4 4 2 2 4 2 2 4 4 4 3 2 3 3 5 3 1 1 4 4 3 3 3 1 5 3 2 2 3 1 3 1 3 4 1 3 4
[297] 2 3 1 2 3 4 3 4 3 1 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4 1 3 1 3 3 3 2 5 4 3 3 3 1 5 3 3 1 2 2 4 5 1 3
[2258] 1 1 1 5 5 1 5 3 1 4 3 3 5 1 5 3 5 5 4 2 5 2 2 1 5 3 3 4 4 3 2 5 1 2 5 2 4
[2295] 4 3 1 2 3 4 1 2 2 1 1 1 1 4 1 2 1 2 1 2 2 1 2 2 2 5 2 2 5 1 1 2 2 2 1 1 1
[2332] 1 5 5 1 2 1 2 1 2 2 1 2 2 2 2 2 5 1 5 5 5 5 5 5 5 1 5 5 5 5 5 5 5 1 5 5 5
[2369] 5 5 5 5 5 5 5 2 5 5 5 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 3 1 1 1 2 1 4 4
[2406] 1 2 1 2 1 1 2 2 2 1 1 2 1 1 4 3 1 2 1 2 1 2 1 1 1 1 1 1 1 1 2 4 1 1 2 4 2
[2443] 1 2 4 1 2 2 1 1 1 4 1 4 1 3 4 1 4 4 1 1 2 1 2 4 2 4 4 4 4 2 4 2 4 4 2 2 2
Within cluster sum of squares by cluster:
[1] 867.4192 572.0308 649.0868 518.2114 533.5076
(between_SS / total_SS = 68.3 %)
Available components:
[1] "cluster" "centers" "totss" "withinss" "tot.withinss"
[6] "betweenss" "size" "iter" "ifault"
Data_Health_Ass4_subset$cluster <- kmeans_results$cluster
table(Data_Health_Ass4_subset$cluster)
1 2 3 4 5
854 417 349 437 422
kmeans_results$centers Stroke BloodPressure Depression HighCholesterol
1 -0.07891134 -0.0120943 0.07622062 0.1274650
2 -1.02793486 -1.2096735 0.18460734 -1.4544704
3 1.50488683 1.4175571 -0.58091886 0.8902141
4 -0.76757364 -0.7793328 -1.18769930 -0.4591696
5 0.72574243 0.8545104 1.37367678 0.9185577
- Run the model with the best estimate number of clusters.
#Est. of # of clusters
library(cluster)
# 1. Force numeric conversion
cluster_vars <- data.frame(
BloodPressure = as.numeric(Data_Health_Ass4$BloodPressure),
Depression = as.numeric(Data_Health_Ass4$Depression),
HighCholesterol = as.numeric(Data_Health_Ass4$HighCholesterol)
)
# 2. Scale the data
scaled_data <- scale(cluster_vars)
# 3. Compute silhouette scores for k = 2 to 6
sil_scores <- sapply(2:6, function(k) {
km <- kmeans(scaled_data, centers = k, nstart = 25)
ss <- silhouette(km$cluster, dist(scaled_data))
mean(ss[, 3])
})
# 4. Best number of clusters
best_k <- which.max(sil_scores) + 1
best_k[1] 2
# 5. Run k-means
kmeans_results <- kmeans(scaled_data, centers = best_k, nstart = 25)
# 6. Add cluster labels
Data_Health_Ass4$cluster <- kmeans_results$cluster
# 7. Regression model
model <- lm(Stroke ~ BloodPressure + Depression + HighCholesterol + factor(cluster),
data = Data_Health_Ass4)
summary(model)
Call:
lm(formula = Stroke ~ BloodPressure + Depression + HighCholesterol +
factor(cluster), data = Data_Health_Ass4)
Residuals:
Min 1Q Median 3Q Max
-1.09963 -0.25613 -0.00842 0.23306 3.08407
Coefficients: (10 not defined because of singularities)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.6382955 0.8988000 1.823 0.068489 .
BloodPressure21.6 0.9460168 0.9993970 0.947 0.343963
BloodPressure22.7 0.9489705 1.1725488 0.809 0.418425
BloodPressure22.8 0.7464106 0.9992806 0.747 0.455182
BloodPressure23 1.0500208 1.1723714 0.896 0.370553
BloodPressure23.1 0.6487736 0.9986783 0.650 0.516004
BloodPressure23.4 0.5465419 0.9992428 0.547 0.584470
BloodPressure23.5 1.1388302 1.0465765 1.088 0.276660
BloodPressure23.6 1.0470670 0.9990967 1.048 0.294759
BloodPressure23.7 0.4471983 0.9990615 0.448 0.654477
BloodPressure23.8 0.9817738 0.9413174 1.043 0.297084
BloodPressure23.9 1.2459281 1.0308859 1.209 0.226959
BloodPressure24.1 1.0130267 0.8985981 1.127 0.259733
BloodPressure24.2 1.4437850 1.0001436 1.444 0.149014
BloodPressure24.3 0.8975868 0.8637036 1.039 0.298823
BloodPressure24.6 1.4370017 0.8959277 1.604 0.108888
BloodPressure24.8 1.0899600 0.8270712 1.318 0.187703
BloodPressure24.9 0.6464763 0.9992616 0.647 0.517736
BloodPressure25 0.8274891 0.8093191 1.022 0.306691
BloodPressure25.1 1.1061314 0.8360846 1.323 0.185990
BloodPressure25.2 0.9303376 0.8648133 1.076 0.282161
BloodPressure25.3 1.1422097 1.0007591 1.141 0.253863
BloodPressure25.4 1.0239949 0.8838248 1.159 0.246759
BloodPressure25.5 1.1664503 0.7970248 1.464 0.143486
BloodPressure25.6 1.0655217 0.8718358 1.222 0.221791
BloodPressure25.7 1.2449666 0.9997300 1.245 0.213166
BloodPressure25.8 1.1877884 0.7978887 1.489 0.136733
BloodPressure25.9 1.0645305 0.8077229 1.318 0.187674
BloodPressure26 1.1607240 0.9054773 1.282 0.200029
BloodPressure26.1 0.7009669 0.8310050 0.844 0.399040
BloodPressure26.2 1.1071213 0.9515449 1.163 0.244766
BloodPressure26.3 1.1973744 0.6146561 1.948 0.051550 .
BloodPressure26.4 0.9522552 0.8078550 1.179 0.238639
BloodPressure26.5 1.2879368 0.8131085 1.584 0.113359
BloodPressure26.6 1.3967540 0.8949558 1.561 0.118753
BloodPressure26.7 1.3601231 0.7996885 1.701 0.089132 .
BloodPressure26.8 1.4589268 0.8243547 1.770 0.076916 .
BloodPressure26.9 1.0900336 0.7981323 1.366 0.172177
BloodPressure27 1.4398898 0.8950780 1.609 0.107845
BloodPressure27.1 1.5446809 0.8280015 1.866 0.062250 .
BloodPressure27.2 1.0173045 0.8023218 1.268 0.204963
BloodPressure27.3 0.2966456 0.9156515 0.324 0.745993
BloodPressure27.4 1.3000669 0.8282508 1.570 0.116653
BloodPressure27.5 1.4545023 0.8035594 1.810 0.070434 .
BloodPressure27.6 1.2338643 0.8011909 1.540 0.123709
BloodPressure27.7 1.5863833 0.8264137 1.920 0.055050 .
BloodPressure27.8 1.1947244 0.8264670 1.446 0.148451
BloodPressure27.9 1.1030740 0.7801833 1.414 0.157557
BloodPressure28 1.1421121 0.8461367 1.350 0.177234
BloodPressure28.1 1.2702493 0.8806053 1.442 0.149325
BloodPressure28.2 1.1100731 0.8023289 1.384 0.166646
BloodPressure28.3 1.1099770 0.9045069 1.227 0.219905
BloodPressure28.4 1.4171463 0.8263770 1.715 0.086520 .
BloodPressure28.5 1.9450147 0.8061480 2.413 0.015923 *
BloodPressure28.6 1.3000357 0.8012723 1.622 0.104861
BloodPressure28.7 1.4729888 0.7872024 1.871 0.061467 .
BloodPressure28.8 1.5522666 0.7833429 1.982 0.047661 *
BloodPressure28.9 1.4071554 0.7781426 1.808 0.070701 .
BloodPressure29 1.5706208 0.7753114 2.026 0.042918 *
BloodPressure29.1 1.4953003 0.7839402 1.907 0.056609 .
BloodPressure29.2 1.2951812 0.7896538 1.640 0.101123
BloodPressure29.3 1.5062470 0.7783316 1.935 0.053103 .
BloodPressure29.4 1.4912061 0.7774150 1.918 0.055233 .
BloodPressure29.5 1.4193309 0.8836976 1.606 0.108403
BloodPressure29.6 1.5514769 0.7829089 1.982 0.047651 *
BloodPressure29.7 1.2824275 0.7737994 1.657 0.097613 .
BloodPressure29.8 1.7447171 0.7754578 2.250 0.024562 *
BloodPressure29.9 1.3623419 0.7716740 1.765 0.077642 .
BloodPressure30 1.6116880 0.7774128 2.073 0.038286 *
BloodPressure30.1 1.6790487 0.7796504 2.154 0.031391 *
BloodPressure30.2 1.3792039 0.7879880 1.750 0.080222 .
BloodPressure30.3 1.7417574 0.7775631 2.240 0.025199 *
BloodPressure30.4 1.6544297 0.7772741 2.129 0.033417 *
BloodPressure30.5 1.7068251 0.7705020 2.215 0.026857 *
BloodPressure30.6 1.5377190 0.7805475 1.970 0.048970 *
BloodPressure30.7 1.9662119 0.7796168 2.522 0.011745 *
BloodPressure30.8 2.0327681 0.7797738 2.607 0.009205 **
BloodPressure30.9 1.7925355 0.7775969 2.305 0.021256 *
BloodPressure31 1.8859279 0.7802586 2.417 0.015735 *
BloodPressure31.1 1.8536383 0.7707173 2.405 0.016259 *
BloodPressure31.2 1.8196289 0.7752589 2.347 0.019016 *
BloodPressure31.3 1.6762716 0.7711159 2.174 0.029835 *
BloodPressure31.4 1.9353455 0.7738558 2.501 0.012467 *
BloodPressure31.5 1.7371199 0.7734894 2.246 0.024824 *
BloodPressure31.6 1.7873214 0.7708239 2.319 0.020511 *
BloodPressure31.7 1.7406273 0.7724245 2.253 0.024338 *
BloodPressure31.8 1.7729868 0.7724833 2.295 0.021826 *
BloodPressure31.9 1.8171685 0.7705155 2.358 0.018450 *
BloodPressure32 2.0467705 0.7707392 2.656 0.007980 **
BloodPressure32.1 1.6919119 0.7683487 2.202 0.027778 *
BloodPressure32.200000000000003 1.9156703 0.7688502 2.492 0.012797 *
BloodPressure32.299999999999997 1.6300444 0.7758267 2.101 0.035762 *
BloodPressure32.4 1.9575934 0.7776574 2.517 0.011903 *
BloodPressure32.5 1.9453489 0.7726651 2.518 0.011889 *
BloodPressure32.6 2.1354236 0.7756897 2.753 0.005960 **
BloodPressure32.700000000000003 1.9075931 0.7770209 2.455 0.014172 *
BloodPressure32.799999999999997 1.7821250 0.7778679 2.291 0.022064 *
BloodPressure32.9 1.9405362 0.7689084 2.524 0.011688 *
BloodPressure33 1.9950930 0.7675333 2.599 0.009408 **
BloodPressure33.1 1.9982044 0.7707214 2.593 0.009593 **
BloodPressure33.200000000000003 2.0617450 0.7664071 2.690 0.007201 **
BloodPressure33.299999999999997 2.1500214 0.7716804 2.786 0.005384 **
BloodPressure33.4 2.0977642 0.7699333 2.725 0.006494 **
BloodPressure33.5 2.1865828 0.7752091 2.821 0.004840 **
BloodPressure33.6 2.3226265 0.7730541 3.004 0.002693 **
BloodPressure33.700000000000003 2.2125853 0.7676690 2.882 0.003991 **
BloodPressure33.799999999999997 1.9716210 0.7708603 2.558 0.010610 *
BloodPressure33.9 1.9298877 0.7725243 2.498 0.012564 *
BloodPressure34 2.2537306 0.7712326 2.922 0.003514 **
BloodPressure34.1 2.0239546 0.7689725 2.632 0.008553 **
BloodPressure34.200000000000003 2.2493867 0.7684208 2.927 0.003458 **
BloodPressure34.299999999999997 2.2757436 0.7695097 2.957 0.003139 **
BloodPressure34.4 2.1438627 0.7689649 2.788 0.005354 **
BloodPressure34.5 2.2311335 0.7704796 2.896 0.003823 **
BloodPressure34.6 2.2510131 0.7667311 2.936 0.003364 **
BloodPressure34.700000000000003 2.2434772 0.7695336 2.915 0.003592 **
BloodPressure34.799999999999997 2.3047025 0.7683565 3.000 0.002737 **
BloodPressure34.9 2.2675165 0.7660979 2.960 0.003114 **
BloodPressure35 2.1649973 0.7689143 2.816 0.004915 **
BloodPressure35.1 2.1317372 0.7680183 2.776 0.005561 **
BloodPressure35.200000000000003 2.1480534 0.7690608 2.793 0.005270 **
BloodPressure35.299999999999997 2.2775706 0.7677066 2.967 0.003046 **
BloodPressure35.4 2.3295625 0.7652504 3.044 0.002363 **
BloodPressure35.5 2.4825595 0.7695311 3.226 0.001275 **
BloodPressure35.6 2.1846919 0.7668069 2.849 0.004429 **
BloodPressure35.700000000000003 2.3064206 0.7676894 3.004 0.002694 **
BloodPressure35.799999999999997 2.2516865 0.7716701 2.918 0.003563 **
BloodPressure35.9 2.4591301 0.7681137 3.202 0.001388 **
BloodPressure36 2.4458035 0.7717150 3.169 0.001551 **
BloodPressure36.1 2.3012735 0.7682397 2.996 0.002773 **
BloodPressure36.200000000000003 2.5159269 0.7753972 3.245 0.001195 **
BloodPressure36.299999999999997 2.4870998 0.7670484 3.242 0.001205 **
BloodPressure36.4 2.5520583 0.7673510 3.326 0.000898 ***
BloodPressure36.5 2.4712226 0.7674263 3.220 0.001302 **
BloodPressure36.6 2.6623130 0.7755060 3.433 0.000609 ***
BloodPressure36.700000000000003 2.6099631 0.7672891 3.402 0.000683 ***
BloodPressure36.799999999999997 2.1785169 0.7669847 2.840 0.004552 **
BloodPressure36.9 2.4633392 0.7657770 3.217 0.001317 **
BloodPressure37 2.4559981 0.7652780 3.209 0.001352 **
BloodPressure37.1 2.4010899 0.7675182 3.128 0.001783 **
BloodPressure37.200000000000003 2.5222778 0.7689316 3.280 0.001055 **
BloodPressure37.299999999999997 2.3940175 0.7669418 3.122 0.001825 **
BloodPressure37.4 2.5323238 0.7709402 3.285 0.001038 **
BloodPressure37.5 2.7047881 0.7684645 3.520 0.000442 ***
BloodPressure37.6 2.7012346 0.7685920 3.515 0.000450 ***
BloodPressure37.700000000000003 2.4501739 0.7677011 3.192 0.001437 **
BloodPressure37.799999999999997 2.6447629 0.7708790 3.431 0.000614 ***
BloodPressure37.9 2.8289453 0.7727687 3.661 0.000258 ***
BloodPressure38 2.5801770 0.7719080 3.343 0.000845 ***
BloodPressure38.1 2.7903185 0.7688973 3.629 0.000292 ***
BloodPressure38.200000000000003 2.5234376 0.7722295 3.268 0.001102 **
BloodPressure38.299999999999997 2.6282635 0.7690215 3.418 0.000644 ***
BloodPressure38.4 2.6498196 0.7677071 3.452 0.000569 ***
BloodPressure38.5 2.5794121 0.7699985 3.350 0.000823 ***
BloodPressure38.6 2.8115586 0.7697315 3.653 0.000266 ***
BloodPressure38.700000000000003 2.6355562 0.7688965 3.428 0.000621 ***
BloodPressure38.799999999999997 2.7722655 0.7728945 3.587 0.000343 ***
BloodPressure38.9 2.6593114 0.7718530 3.445 0.000582 ***
BloodPressure39 2.7854141 0.7690708 3.622 0.000300 ***
BloodPressure39.1 2.7205970 0.7700195 3.533 0.000420 ***
BloodPressure39.200000000000003 2.6746921 0.7688709 3.479 0.000515 ***
BloodPressure39.299999999999997 2.8641897 0.7695086 3.722 0.000203 ***
BloodPressure39.4 2.8266521 0.7693466 3.674 0.000245 ***
BloodPressure39.5 2.6850683 0.7675713 3.498 0.000479 ***
BloodPressure39.6 2.7315574 0.7659586 3.566 0.000371 ***
BloodPressure39.700000000000003 2.6407721 0.7721554 3.420 0.000639 ***
BloodPressure39.799999999999997 2.6266060 0.7711277 3.406 0.000672 ***
BloodPressure39.9 2.8125837 0.7714185 3.646 0.000273 ***
BloodPressure40 2.6647324 0.7714836 3.454 0.000564 ***
BloodPressure40.1 2.9642294 0.7733179 3.833 0.000130 ***
BloodPressure40.200000000000003 2.7778092 0.7727795 3.595 0.000333 ***
BloodPressure40.299999999999997 2.7856029 0.7713981 3.611 0.000312 ***
BloodPressure40.4 2.8853360 0.7705662 3.744 0.000186 ***
BloodPressure40.5 2.9720231 0.7693137 3.863 0.000115 ***
BloodPressure40.6 2.7862353 0.7719394 3.609 0.000314 ***
BloodPressure40.700000000000003 2.9416744 0.8036305 3.660 0.000258 ***
BloodPressure40.799999999999997 2.9035267 0.7688598 3.776 0.000164 ***
BloodPressure40.9 3.1024071 0.7714360 4.022 5.99e-05 ***
BloodPressure41 2.8655971 0.7711476 3.716 0.000208 ***
BloodPressure41.1 3.0546646 0.7694430 3.970 7.44e-05 ***
BloodPressure41.2 2.8806741 0.7709369 3.737 0.000192 ***
BloodPressure41.3 2.8498071 0.7748054 3.678 0.000241 ***
BloodPressure41.4 3.0600639 0.7709610 3.969 7.47e-05 ***
BloodPressure41.5 3.1234534 0.7715893 4.048 5.36e-05 ***
BloodPressure41.6 3.2864536 0.7746618 4.242 2.31e-05 ***
BloodPressure41.7 3.2409681 0.7798526 4.156 3.38e-05 ***
BloodPressure41.8 3.1113842 0.7722218 4.029 5.81e-05 ***
BloodPressure41.9 3.0382226 0.7732630 3.929 8.82e-05 ***
BloodPressure42 3.2869522 0.7708145 4.264 2.10e-05 ***
BloodPressure42.1 3.0057879 0.7713522 3.897 0.000101 ***
BloodPressure42.2 2.9634971 0.7726582 3.835 0.000129 ***
BloodPressure42.3 3.1918513 0.7746796 4.120 3.94e-05 ***
BloodPressure42.4 3.1224249 0.7733363 4.038 5.60e-05 ***
BloodPressure42.5 3.1893261 0.7727475 4.127 3.82e-05 ***
BloodPressure42.6 2.9549147 0.7711008 3.832 0.000131 ***
BloodPressure42.7 3.1849145 0.7724590 4.123 3.89e-05 ***
BloodPressure42.8 3.2779743 0.7746207 4.232 2.42e-05 ***
BloodPressure42.9 3.3430809 0.7733807 4.323 1.62e-05 ***
BloodPressure43 3.5063723 0.7811598 4.489 7.57e-06 ***
BloodPressure43.1 3.3146325 0.7778962 4.261 2.13e-05 ***
BloodPressure43.2 3.2506449 0.7689521 4.227 2.47e-05 ***
BloodPressure43.3 3.3972342 0.7734169 4.393 1.18e-05 ***
BloodPressure43.4 3.4443618 0.7703502 4.471 8.21e-06 ***
BloodPressure43.5 3.4997549 0.7723406 4.531 6.21e-06 ***
BloodPressure43.6 3.4692848 0.7747341 4.478 7.96e-06 ***
BloodPressure43.7 3.6638073 0.7744977 4.731 2.40e-06 ***
BloodPressure43.8 3.1192286 0.7759273 4.020 6.03e-05 ***
BloodPressure43.9 3.4944435 0.7820360 4.468 8.32e-06 ***
BloodPressure44 3.4679179 0.7935310 4.370 1.30e-05 ***
BloodPressure44.1 3.6094167 0.7748296 4.658 3.40e-06 ***
BloodPressure44.2 3.5515011 0.7749983 4.583 4.87e-06 ***
BloodPressure44.3 3.5163097 0.7842344 4.484 7.75e-06 ***
BloodPressure44.4 3.4647291 0.7829821 4.425 1.02e-05 ***
BloodPressure44.5 3.5448022 0.7809921 4.539 5.99e-06 ***
BloodPressure44.6 3.6260068 0.7907933 4.585 4.81e-06 ***
BloodPressure44.7 3.6166619 0.7736799 4.675 3.14e-06 ***
BloodPressure44.8 4.0822552 0.7795726 5.237 1.81e-07 ***
BloodPressure44.9 3.7293554 0.7744970 4.815 1.58e-06 ***
BloodPressure45 3.5680223 0.7797589 4.576 5.03e-06 ***
BloodPressure45.1 3.6569476 0.7713234 4.741 2.27e-06 ***
BloodPressure45.2 3.6642051 0.7877905 4.651 3.51e-06 ***
BloodPressure45.3 3.6842352 0.7766289 4.744 2.24e-06 ***
BloodPressure45.4 3.8017150 0.7739975 4.912 9.76e-07 ***
BloodPressure45.5 3.6326654 0.7810520 4.651 3.52e-06 ***
BloodPressure45.6 3.9690284 0.7773686 5.106 3.61e-07 ***
BloodPressure45.7 3.6483509 0.8166525 4.467 8.36e-06 ***
BloodPressure45.8 3.5741804 0.7933002 4.505 7.00e-06 ***
BloodPressure45.9 3.4824639 0.7969636 4.370 1.31e-05 ***
BloodPressure46 3.5166545 0.7871957 4.467 8.36e-06 ***
BloodPressure46.1 4.1624147 0.7935948 5.245 1.73e-07 ***
BloodPressure46.2 4.0510852 0.8236595 4.918 9.44e-07 ***
BloodPressure46.3 3.8241164 0.7788219 4.910 9.84e-07 ***
BloodPressure46.4 4.2476729 0.7892868 5.382 8.24e-08 ***
BloodPressure46.5 3.8723884 0.7778066 4.979 6.95e-07 ***
BloodPressure46.6 3.9481317 0.7815869 5.051 4.78e-07 ***
BloodPressure46.7 3.6547275 0.8477824 4.311 1.70e-05 ***
BloodPressure46.8 3.9473819 0.7837741 5.036 5.17e-07 ***
BloodPressure46.9 3.9836238 0.7873286 5.060 4.58e-07 ***
BloodPressure47 4.3552211 0.8074473 5.394 7.71e-08 ***
BloodPressure47.1 3.9591518 0.7929357 4.993 6.46e-07 ***
BloodPressure47.2 3.9801554 0.8229007 4.837 1.42e-06 ***
BloodPressure47.3 3.8890031 0.7870102 4.941 8.40e-07 ***
BloodPressure47.4 4.0205248 0.7955985 5.053 4.73e-07 ***
BloodPressure47.5 3.9639669 0.7943224 4.990 6.55e-07 ***
BloodPressure47.6 3.6581911 0.8231058 4.444 9.30e-06 ***
BloodPressure47.7 4.5810372 0.7821581 5.857 5.50e-09 ***
BloodPressure47.8 3.8748105 0.7870367 4.923 9.21e-07 ***
BloodPressure47.9 4.0561998 0.8794679 4.612 4.24e-06 ***
BloodPressure48 4.2876286 0.8242733 5.202 2.18e-07 ***
BloodPressure48.1 3.9347941 0.7971950 4.936 8.64e-07 ***
BloodPressure48.2 4.0468899 0.7866267 5.145 2.94e-07 ***
BloodPressure48.3 4.1635255 0.8093659 5.144 2.95e-07 ***
BloodPressure48.4 4.6427833 0.8029944 5.782 8.55e-09 ***
BloodPressure48.5 4.2018080 0.8329100 5.045 4.95e-07 ***
BloodPressure48.6 4.7330944 0.8277238 5.718 1.24e-08 ***
BloodPressure48.7 4.1593198 0.8185718 5.081 4.10e-07 ***
BloodPressure48.8 3.5500574 0.8811876 4.029 5.82e-05 ***
BloodPressure48.9 4.3664355 0.7935068 5.503 4.22e-08 ***
BloodPressure49 5.1754762 0.8299084 6.236 5.45e-10 ***
BloodPressure49.1 4.3814805 0.7961154 5.504 4.20e-08 ***
BloodPressure49.2 4.0923082 0.8831780 4.634 3.82e-06 ***
BloodPressure49.4 4.3932544 0.8027312 5.473 4.98e-08 ***
BloodPressure49.5 4.2498492 0.8365021 5.081 4.11e-07 ***
BloodPressure49.6 4.3529951 0.7861616 5.537 3.48e-08 ***
BloodPressure49.7 4.0910321 0.8222348 4.976 7.06e-07 ***
BloodPressure49.8 5.3693214 0.8370065 6.415 1.75e-10 ***
BloodPressure49.9 5.2064263 0.8115825 6.415 1.75e-10 ***
BloodPressure50 4.7545781 0.8063087 5.897 4.34e-09 ***
BloodPressure50.2 3.9638998 0.8802442 4.503 7.08e-06 ***
BloodPressure50.3 5.0498913 0.8798730 5.739 1.10e-08 ***
BloodPressure50.4 3.9353782 0.8227670 4.783 1.85e-06 ***
BloodPressure50.6 4.6116201 0.8229209 5.604 2.38e-08 ***
BloodPressure50.7 4.6527261 0.8231132 5.653 1.81e-08 ***
BloodPressure50.8 4.6165464 0.7951729 5.806 7.43e-09 ***
BloodPressure51 4.9154563 0.8810380 5.579 2.74e-08 ***
BloodPressure51.1 4.7542756 0.8990360 5.288 1.37e-07 ***
BloodPressure51.2 5.3261798 0.8870702 6.004 2.28e-09 ***
BloodPressure51.4 5.5296640 0.8820938 6.269 4.44e-10 ***
BloodPressure51.5 4.9583267 0.8799472 5.635 2.00e-08 ***
BloodPressure51.6 5.3438585 0.9019634 5.925 3.67e-09 ***
BloodPressure51.8 4.7569528 0.8226897 5.782 8.53e-09 ***
BloodPressure51.9 5.0316125 0.8802128 5.716 1.25e-08 ***
BloodPressure52 4.9793914 0.8226421 6.053 1.69e-09 ***
BloodPressure52.2 4.6330984 0.8964076 5.169 2.59e-07 ***
BloodPressure52.4 5.2789368 0.8226020 6.417 1.73e-10 ***
BloodPressure52.6 4.7646875 0.8801082 5.414 6.91e-08 ***
BloodPressure52.7 5.5124193 0.8815060 6.253 4.89e-10 ***
BloodPressure52.9 5.0579064 0.8791191 5.753 1.01e-08 ***
BloodPressure53 5.3895939 0.9983975 5.398 7.53e-08 ***
BloodPressure53.1 4.7586956 0.8798493 5.409 7.11e-08 ***
BloodPressure53.2 5.4925611 0.8866096 6.195 7.05e-10 ***
BloodPressure53.5 5.0978555 0.8868349 5.748 1.04e-08 ***
BloodPressure53.8 5.3783750 0.8041931 6.688 2.93e-11 ***
BloodPressure53.9 5.5152593 0.8810783 6.260 4.70e-10 ***
BloodPressure54.5 5.4944111 0.8793858 6.248 5.06e-10 ***
BloodPressure54.6 5.8309776 0.8786712 6.636 4.13e-11 ***
BloodPressure55.1 6.4786389 0.8795146 7.366 2.55e-13 ***
BloodPressure55.5 7.0681292 0.9111598 7.757 1.37e-14 ***
BloodPressure55.8 5.2584186 0.8815211 5.965 2.88e-09 ***
BloodPressure57.7 6.4206890 0.8852131 7.253 5.78e-13 ***
Depression 0.0006564 0.0035750 0.184 0.854340
HighCholesterol25.2 NA NA NA NA
HighCholesterol25.5 NA NA NA NA
HighCholesterol26.4 -0.0435494 0.7262982 -0.060 0.952193
HighCholesterol26.6 -0.2014441 0.6145401 -0.328 0.743100
HighCholesterol26.7 NA NA NA NA
HighCholesterol26.8 -0.3552894 0.5978494 -0.594 0.552393
HighCholesterol26.9 0.2453604 0.7884194 0.311 0.755677
HighCholesterol27 NA NA NA NA
HighCholesterol27.3 0.8165982 0.7676127 1.064 0.287540
HighCholesterol27.4 NA NA NA NA
HighCholesterol27.6 -0.0032820 0.8692036 -0.004 0.996988
HighCholesterol27.7 NA NA NA NA
HighCholesterol27.9 -0.8271689 0.6790767 -1.218 0.223337
HighCholesterol28 -0.0211617 0.6661788 -0.032 0.974662
HighCholesterol28.1 -0.4258512 0.6651234 -0.640 0.522077
HighCholesterol28.2 0.0041770 0.6337706 0.007 0.994742
HighCholesterol28.3 NA NA NA NA
HighCholesterol28.4 0.2181339 0.7054480 0.309 0.757191
HighCholesterol28.5 -0.2596828 0.6726973 -0.386 0.699514
HighCholesterol28.7 -0.2371354 0.5920246 -0.401 0.688794
HighCholesterol28.8 0.0777942 0.6908558 0.113 0.910355
HighCholesterol28.9 -0.0935354 0.7460465 -0.125 0.900239
HighCholesterol29 NA NA NA NA
HighCholesterol29.1 -0.0048160 0.5931836 -0.008 0.993523
HighCholesterol29.2 -0.2655659 0.5219112 -0.509 0.610925
HighCholesterol29.3 -0.2671486 0.5641566 -0.474 0.635882
HighCholesterol29.4 -0.4982047 0.7260431 -0.686 0.492672
HighCholesterol29.5 -0.0137343 0.5692349 -0.024 0.980753
HighCholesterol29.6 -0.1724467 0.5469316 -0.315 0.752568
HighCholesterol29.7 0.0880401 0.5267154 0.167 0.867269
HighCholesterol29.8 2.5407235 0.6556726 3.875 0.000110 ***
HighCholesterol29.9 0.1383703 0.6077428 0.228 0.819919
HighCholesterol30 -0.9623676 0.6552456 -1.469 0.142068
HighCholesterol30.1 -0.1457467 0.5728442 -0.254 0.799192
HighCholesterol30.2 -0.2774369 0.5271144 -0.526 0.598716
HighCholesterol30.3 0.2987738 0.5924303 0.504 0.614093
HighCholesterol30.4 -0.0102384 0.5271304 -0.019 0.984506
HighCholesterol30.5 -0.1758148 0.5163797 -0.340 0.733534
HighCholesterol30.6 0.3606456 0.5291355 0.682 0.495586
HighCholesterol30.7 1.1504871 0.5503291 2.091 0.036695 *
HighCholesterol30.8 -0.1813218 0.5208128 -0.348 0.727763
HighCholesterol30.9 0.1478597 0.5234970 0.282 0.777631
HighCholesterol31 0.0822105 0.5769992 0.142 0.886716
HighCholesterol31.1 -0.2496214 0.5155516 -0.484 0.628309
HighCholesterol31.2 -0.0023998 0.5127967 -0.005 0.996267
HighCholesterol31.3 -0.2236480 0.5224868 -0.428 0.668664
HighCholesterol31.4 -0.2716104 0.5120315 -0.530 0.595854
HighCholesterol31.5 0.1853584 0.5331984 0.348 0.728151
HighCholesterol31.6 0.2447629 0.5021065 0.487 0.625977
HighCholesterol31.7 0.2342019 0.5293476 0.442 0.658222
HighCholesterol31.8 -0.0863812 0.5090401 -0.170 0.865268
HighCholesterol31.9 0.0349887 0.5011883 0.070 0.944351
HighCholesterol32 0.1329331 0.4991731 0.266 0.790031
HighCholesterol32.1 -0.0321364 0.5000934 -0.064 0.948769
HighCholesterol32.200000000000003 -0.0767310 0.4951545 -0.155 0.876865
HighCholesterol32.299999999999997 0.0421109 0.4941638 0.085 0.932098
HighCholesterol32.4 0.0273423 0.4941935 0.055 0.955883
HighCholesterol32.5 -0.0932166 0.5139726 -0.181 0.856099
HighCholesterol32.6 0.1113514 0.4912350 0.227 0.820698
HighCholesterol32.700000000000003 -0.0971403 0.5226921 -0.186 0.852584
HighCholesterol32.799999999999997 -0.1395298 0.4963417 -0.281 0.778650
HighCholesterol32.9 -0.2322673 0.4990621 -0.465 0.641690
HighCholesterol33 -0.3748686 0.5050332 -0.742 0.458014
HighCholesterol33.1 -0.3459347 0.4961811 -0.697 0.485762
HighCholesterol33.200000000000003 -0.3043188 0.4956420 -0.614 0.539292
HighCholesterol33.299999999999997 -0.1074147 0.4975968 -0.216 0.829113
HighCholesterol33.4 0.1360394 0.4905517 0.277 0.781564
HighCholesterol33.5 -0.0937469 0.4919403 -0.191 0.848885
HighCholesterol33.6 0.0095556 0.4908179 0.019 0.984469
HighCholesterol33.700000000000003 0.0153699 0.4900759 0.031 0.974984
HighCholesterol33.799999999999997 -0.2689613 0.4940566 -0.544 0.586231
HighCholesterol33.9 -0.0801359 0.4942926 -0.162 0.871226
HighCholesterol34 -0.1943405 0.4883199 -0.398 0.690689
HighCholesterol34.1 -0.1457783 0.4905621 -0.297 0.766371
HighCholesterol34.200000000000003 0.0912331 0.4975958 0.183 0.854544
HighCholesterol34.299999999999997 -0.1692573 0.4922363 -0.344 0.730992
HighCholesterol34.4 -0.2244931 0.4889240 -0.459 0.646171
HighCholesterol34.5 0.0395173 0.4904581 0.081 0.935790
HighCholesterol34.6 -0.0461007 0.4900230 -0.094 0.925056
HighCholesterol34.700000000000003 -0.1551825 0.4889897 -0.317 0.751009
HighCholesterol34.799999999999997 0.0577266 0.4907656 0.118 0.906376
HighCholesterol34.9 -0.0209505 0.4889714 -0.043 0.965828
HighCholesterol35 -0.2457434 0.4897576 -0.502 0.615888
HighCholesterol35.1 -0.1357424 0.4879671 -0.278 0.780903
HighCholesterol35.200000000000003 -0.2305516 0.4876984 -0.473 0.636454
HighCholesterol35.299999999999997 -0.0682077 0.4864680 -0.140 0.888508
HighCholesterol35.4 -0.0710539 0.4875964 -0.146 0.884155
HighCholesterol35.5 -0.1385183 0.4857050 -0.285 0.775528
HighCholesterol35.6 -0.2294708 0.4864288 -0.472 0.637159
HighCholesterol35.700000000000003 -0.1413432 0.4888083 -0.289 0.772490
HighCholesterol35.799999999999997 -0.2017713 0.4879863 -0.413 0.679301
HighCholesterol35.9 0.0001785 0.4949963 0.000 0.999712
HighCholesterol36 -0.1757159 0.4861687 -0.361 0.717816
HighCholesterol36.1 -0.1038326 0.4859901 -0.214 0.830840
HighCholesterol36.200000000000003 -0.1109725 0.4849279 -0.229 0.819014
HighCholesterol36.299999999999997 -0.0194403 0.4824719 -0.040 0.967863
HighCholesterol36.4 -0.0199264 0.4852435 -0.041 0.967248
HighCholesterol36.5 0.0871403 0.4896058 0.178 0.858756
HighCholesterol36.6 -0.0681828 0.4865028 -0.140 0.888556
HighCholesterol36.700000000000003 -0.2408586 0.4843317 -0.497 0.619031
HighCholesterol36.799999999999997 -0.0963186 0.4859641 -0.198 0.842908
HighCholesterol36.9 -0.0584292 0.4858389 -0.120 0.904286
HighCholesterol37 0.0874949 0.4867486 0.180 0.857364
HighCholesterol37.1 -0.1219093 0.4869542 -0.250 0.802342
HighCholesterol37.200000000000003 -0.0367944 0.4861633 -0.076 0.939679
HighCholesterol37.299999999999997 0.1309655 0.4848496 0.270 0.787099
HighCholesterol37.4 -0.0893763 0.4865962 -0.184 0.854286
HighCholesterol37.5 0.0939381 0.4911114 0.191 0.848328
HighCholesterol37.6 -0.0798539 0.4863774 -0.164 0.869605
HighCholesterol37.700000000000003 -0.1013853 0.4858312 -0.209 0.834716
HighCholesterol37.799999999999997 -0.0639424 0.4876118 -0.131 0.895683
HighCholesterol37.9 -0.0578268 0.4836311 -0.120 0.904837
HighCholesterol38 -0.0601661 0.4866798 -0.124 0.901624
HighCholesterol38.1 0.1252346 0.4871693 0.257 0.797154
HighCholesterol38.200000000000003 0.0104955 0.4857856 0.022 0.982765
HighCholesterol38.299999999999997 -0.0268470 0.4882675 -0.055 0.956157
HighCholesterol38.4 -0.0379570 0.4852464 -0.078 0.937659
HighCholesterol38.5 0.1311096 0.4892933 0.268 0.788760
HighCholesterol38.6 0.1367000 0.4855164 0.282 0.778313
HighCholesterol38.700000000000003 -0.0485890 0.4869172 -0.100 0.920522
HighCholesterol38.799999999999997 -0.0029515 0.4866772 -0.006 0.995162
HighCholesterol38.9 -0.0546952 0.4885591 -0.112 0.910873
HighCholesterol39 0.1556184 0.4833475 0.322 0.747517
HighCholesterol39.1 -0.0472083 0.4913108 -0.096 0.923462
HighCholesterol39.200000000000003 0.1601777 0.4863696 0.329 0.741938
HighCholesterol39.299999999999997 0.3004017 0.4902306 0.613 0.540094
HighCholesterol39.4 0.1571009 0.4911209 0.320 0.749091
HighCholesterol39.5 0.0711053 0.4880000 0.146 0.884167
HighCholesterol39.6 0.0803250 0.4876651 0.165 0.869186
HighCholesterol39.700000000000003 0.0293689 0.4870600 0.060 0.951924
HighCholesterol39.799999999999997 -0.0382518 0.4862983 -0.079 0.937312
HighCholesterol39.9 -0.0463683 0.4873100 -0.095 0.924204
HighCholesterol40 -0.0430239 0.4894071 -0.088 0.929957
HighCholesterol40.1 -0.0247946 0.4888150 -0.051 0.959551
HighCholesterol40.200000000000003 0.3037900 0.4890457 0.621 0.534546
HighCholesterol40.299999999999997 0.0603164 0.4982054 0.121 0.903650
HighCholesterol40.4 -0.0020482 0.4986947 -0.004 0.996723
HighCholesterol40.5 0.0182684 0.4872036 0.037 0.970093
HighCholesterol40.6 0.0831208 0.4902747 0.170 0.865390
HighCholesterol40.700000000000003 0.1509288 0.4950798 0.305 0.760506
HighCholesterol40.799999999999997 0.0623964 0.4874113 0.128 0.898149
HighCholesterol40.9 0.0601743 0.5027810 0.120 0.904746
HighCholesterol41 0.3301954 0.4960267 0.666 0.505692
HighCholesterol41.1 0.0678546 0.4951163 0.137 0.891007
HighCholesterol41.2 0.1003990 0.5070449 0.198 0.843059
HighCholesterol41.3 0.2171184 0.4956655 0.438 0.661409
HighCholesterol41.4 0.2911476 0.4981095 0.585 0.558946
HighCholesterol41.5 -0.3464595 0.5144620 -0.673 0.500745
HighCholesterol41.6 0.2731259 0.5222291 0.523 0.601032
HighCholesterol41.7 0.0325614 0.5200562 0.063 0.950082
HighCholesterol41.8 0.1632259 0.5026046 0.325 0.745396
HighCholesterol41.9 0.0633958 0.5120594 0.124 0.901482
HighCholesterol42 0.1130381 0.5094100 0.222 0.824414
HighCholesterol42.1 0.0419940 0.5235922 0.080 0.936083
HighCholesterol42.2 0.2180790 0.5369892 0.406 0.684702
HighCholesterol42.3 0.1592244 0.5329055 0.299 0.765135
HighCholesterol42.4 -0.0791437 0.5103533 -0.155 0.876777
HighCholesterol42.5 0.2524116 0.5158042 0.489 0.624644
HighCholesterol42.6 -0.0369141 0.4989513 -0.074 0.941031
HighCholesterol42.7 0.0136936 0.5467434 0.025 0.980021
HighCholesterol42.8 0.2568893 0.5007668 0.513 0.608013
HighCholesterol42.9 -0.5272338 0.5580035 -0.945 0.344845
HighCholesterol43 0.0208083 0.5400091 0.039 0.969266
HighCholesterol43.1 0.2065784 0.6779031 0.305 0.760602
HighCholesterol43.2 -0.1554252 0.5218279 -0.298 0.765850
HighCholesterol43.3 0.0668470 0.5138782 0.130 0.896514
HighCholesterol43.4 0.2504646 0.5402929 0.464 0.643005
HighCholesterol43.5 0.0396424 0.5350423 0.074 0.940945
HighCholesterol43.6 0.5098162 0.5616323 0.908 0.364125
HighCholesterol43.7 -0.2253475 0.6029621 -0.374 0.708642
HighCholesterol43.8 0.7137266 0.6659078 1.072 0.283934
HighCholesterol43.9 -0.0375014 0.5674197 -0.066 0.947312
HighCholesterol44 0.0231610 0.6617746 0.035 0.972085
HighCholesterol44.1 -0.0067388 0.5629672 -0.012 0.990451
HighCholesterol44.3 NA NA NA NA
HighCholesterol44.8 0.7011348 0.6592149 1.064 0.287642
HighCholesterol44.9 -0.0189919 0.5307879 -0.036 0.971461
HighCholesterol45.1 0.3366268 0.5896503 0.571 0.568137
HighCholesterol45.4 -0.1234325 0.5884559 -0.210 0.833879
HighCholesterol45.6 -0.1544370 0.5903925 -0.262 0.793669
HighCholesterol46 NA NA NA NA
factor(cluster)2 -0.0406890 0.0443321 -0.918 0.358822
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4345 on 2006 degrees of freedom
Multiple R-squared: 0.8258, Adjusted R-squared: 0.7848
F-statistic: 20.15 on 472 and 2006 DF, p-value: < 2.2e-16
- Show the average summary of variables in each cluster.
library(cluster)
# Convert all columns except County and cluster to numeric
Data_Health_Ass4[ , !(names(Data_Health_Ass4) %in% c("County", "cluster")) ] <-
lapply(Data_Health_Ass4[ , !(names(Data_Health_Ass4) %in% c("County", "cluster"))
],
function(x) as.numeric(as.character(x)))
aggregate(. ~ cluster, data = Data_Health_Ass4[ , -1] , FUN = mean) cluster Arthritis Asthma BloodPressure Chlamydia COPD Depression
1 1 27.74958 10.50042 33.06655 376.0425 7.296891 22.59336
2 2 34.18929 11.11528 41.80209 399.8197 10.614275 24.79310
Gonorrhea Heart HighCholesterol HIV Stroke Syphilis
1 108.5097 7.119160 34.25765 193.7540 3.583277 13.47857
2 126.1157 9.223584 39.05353 216.1456 4.805896 14.52273
Show the states in the healthiest cluster.