You are on page 1of 7

ok5.

PIUS

2022-11-28

# importing the bmd dataset


library(readxl)
bmd_9_ <- read_excel("AST 3.2/bmd (9).xlsx")
View(bmd_9_)
str(bmd_9_)

## tibble [169 x 9] (S3: tbl_df/tbl/data.frame)


## $ id : num [1:169] 469 8724 6736 24180 17072 ...
## $ age : num [1:169] 57.1 75.7 70.8 78.2 54.2 ...
## $ sex : chr [1:169] "F" "F" "M" "F" ...
## $ fracture : chr [1:169] "no fracture" "no fracture" "no fracture" "no fracture" ...
## $ weight_kg : num [1:169] 64 78 73 60 55 65 77 59 64 72 ...
## $ height_cm : num [1:169] 156 162 170 148 161 ...
## $ medication : chr [1:169] "Anticonvulsant" "No medication" "No medication" "No medication" ...
## $ waiting_time: num [1:169] 18 56 10 14 20 7 26 9 6 10 ...
## $ bmd : num [1:169] 0.879 0.795 0.907 0.711 0.791 ...

#.Provide a summary statistics for all the variables in the dataset


bmd_9_$sex = as.factor(bmd_9_$sex)
bmd_9_$fracture = as.factor(bmd_9_$fracture)
bmd_9_$medication = as.factor(bmd_9_$medication)
summary(bmd_9_)

## id age sex fracture weight_kg height_cm


## Min. : 35 Min. :35.81 F:83 fracture : 50 Min. :36.00 Min. :142.0
## 1st Qu.: 2018 1st Qu.:54.42 M:86 no fracture:119 1st Qu.:56.00 1st Qu.:154.0
## Median : 6702 Median :63.49 Median :64.50 Median :160.5
## Mean : 9103 Mean :63.63 Mean :64.67 Mean :160.3
## 3rd Qu.:17100 3rd Qu.:72.08 3rd Qu.:73.00 3rd Qu.:166.0
## Max. :24208 Max. :88.75 Max. :96.00 Max. :177.0
## medication waiting_time bmd
## Anticonvulsant : 9 Min. : 5.00 Min. :0.4076
## Glucocorticoids: 24 1st Qu.: 9.00 1st Qu.:0.6708
## No medication :136 Median :14.00 Median :0.7861
## Mean :19.74 Mean :0.7831
## 3rd Qu.:24.00 3rd Qu.:0.8888
## Max. :96.00 Max. :1.3624

#2.Fitseveral simple logistic regression models with fracture as outcome and age,sex,bmi=(weight_kg/hei
#creating a new variable bmi and bind it with the dateset

1
bmi= bmd_9_$weight_kg/(bmd_9_$ height_cmˆ2)
bmd_9_1 = cbind(bmd_9_,bmi)
str(bmd_9_1)

## ’data.frame’: 169 obs. of 10 variables:


## $ id : num 469 8724 6736 24180 17072 ...
## $ age : num 57.1 75.7 70.8 78.2 54.2 ...
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 1 2 2 2 1 2 2 ...
## $ fracture : Factor w/ 2 levels "fracture","no fracture": 2 2 2 2 2 2 2 2 2 2 ...
## $ weight_kg : num 64 78 73 60 55 65 77 59 64 72 ...
## $ height_cm : num 156 162 170 148 161 ...
## $ medication : Factor w/ 3 levels "Anticonvulsant",..: 1 3 3 3 3 3 3 3 2 3 ...
## $ waiting_time: num 18 56 10 14 20 7 26 9 6 10 ...
## $ bmd : num 0.879 0.795 0.907 0.711 0.791 ...
## $ bmi : num 0.00265 0.00297 0.00251 0.00274 0.00212 ...

#fitting several logistic regression model wth fracture as


#the outcome variable and the rest as predictor
model_age = glm(fracture~age, data = bmd_9_1, family = binomial)
summary(model_age)

##
## Call:
## glm(formula = fracture ~ age, family = binomial, data = bmd_9_1)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.3290 -1.0699 0.6217 0.8552 1.4446
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 5.01898 1.07675 4.661 3.14e-06 ***
## age -0.06341 0.01583 -4.007 6.15e-05 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 205.27 on 168 degrees of freedom
## Residual deviance: 186.75 on 167 degrees of freedom
## AIC: 190.75
##
## Number of Fisher Scoring iterations: 4

model_sex = glm(fracture~sex, data = bmd_9_1,family = binomial)


summary(model_sex)

##
## Call:
## glm(formula = fracture ~ sex, family = binomial, data = bmd_9_1)
##
## Deviance Residuals:

2
## Min 1Q Median 3Q Max
## -1.5719 -1.5492 0.8288 0.8466 0.8466
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.84157 0.23925 3.517 0.000436 ***
## sexM 0.05043 0.33710 0.150 0.881078
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 205.27 on 168 degrees of freedom
## Residual deviance: 205.25 on 167 degrees of freedom
## AIC: 209.25
##
## Number of Fisher Scoring iterations: 4

model_bmi = glm(fracture~bmi, data = bmd_9_1, family = binomial)


summary(model_bmi)

##
## Call:
## glm(formula = fracture ~ bmi, family = binomial, data = bmd_9_1)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.7180 -1.1338 0.6310 0.8163 1.5906
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -4.004 1.212 -3.305 0.00095 ***
## bmi 1990.643 501.959 3.966 7.32e-05 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 205.27 on 168 degrees of freedom
## Residual deviance: 185.52 on 167 degrees of freedom
## AIC: 189.52
##
## Number of Fisher Scoring iterations: 4

model_medication = glm(fracture~medication,data = bmd_9_1, family = binomial)


summary(model_medication)

##
## Call:
## glm(formula = fracture ~ medication, family = binomial, data = bmd_9_1)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max

3
## -1.7712 -1.5486 0.8471 0.8471 1.0842
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.2231 0.6708 0.333 0.739
## medicationGlucocorticoids 1.1119 0.8382 1.326 0.185
## medicationNo medication 0.6172 0.6964 0.886 0.375
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 205.27 on 168 degrees of freedom
## Residual deviance: 203.42 on 166 degrees of freedom
## AIC: 209.42
##
## Number of Fisher Scoring iterations: 4

model_waiting_time = glm(fracture~waiting_time, data = bmd_9_1, family = binomial)


summary(model_waiting_time)

##
## Call:
## glm(formula = fracture ~ waiting_time, family = binomial, data = bmd_9_1)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.6850 -1.4114 0.7628 0.8208 1.3661
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.25668 0.27556 4.560 5.11e-06 ***
## waiting_time -0.01899 0.01028 -1.847 0.0648 .
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 205.27 on 168 degrees of freedom
## Residual deviance: 201.85 on 167 degrees of freedom
## AIC: 205.85
##
## Number of Fisher Scoring iterations: 4

model_bmd = glm(fracture~bmd, data = bmd_9_1, family = binomial)


summary(model_bmd)

##
## Call:
## glm(formula = fracture ~ bmd, family = binomial, data = bmd_9_1)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4960 -0.4453 0.2083 0.5191 2.3003
##

4
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -10.607 1.767 -6.002 1.95e-09 ***
## bmd 15.785 2.497 6.321 2.59e-10 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 205.27 on 168 degrees of freedom
## Residual deviance: 116.02 on 167 degrees of freedom
## AIC: 120.02
##
## Number of Fisher Scoring iterations: 6

#a a.Do you think that there is a relationship between the outcome and each of the variables?
#considering the p-value in the above logistic regression models,the p-values of age,bmi and

#b Test the significance of each of the predictor?


#c Which variables would you consider in a multiple logistic regression

#3.What's the proportion of respondents who have a fracture? What are the odds of having fracture given

# 4.Fitting a multiple logistic regression model with fracture as outcome and age, sex,bmi medication,
#a.Assess correlation between the 4 variables age, sex,bmi, medication, waiting_time and bmd
bmi= bmd_9_$weight_kg/(bmd_9_$ height_cmˆ2)
bmd_9_1 = cbind(bmd_9_,bmi)
str(bmd_9_1)

## ’data.frame’: 169 obs. of 10 variables:


## $ id : num 469 8724 6736 24180 17072 ...
## $ age : num 57.1 75.7 70.8 78.2 54.2 ...
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 1 2 2 2 1 2 2 ...
## $ fracture : Factor w/ 2 levels "fracture","no fracture": 2 2 2 2 2 2 2 2 2 2 ...
## $ weight_kg : num 64 78 73 60 55 65 77 59 64 72 ...
## $ height_cm : num 156 162 170 148 161 ...
## $ medication : Factor w/ 3 levels "Anticonvulsant",..: 1 3 3 3 3 3 3 3 2 3 ...
## $ waiting_time: num 18 56 10 14 20 7 26 9 6 10 ...
## $ bmd : num 0.879 0.795 0.907 0.711 0.791 ...
## $ bmi : num 0.00265 0.00297 0.00251 0.00274 0.00212 ...

bmd_9_2 = dummify(bmd_9_1,select = c("sex","medication"))


str(bmd_9_2)

## ’data.frame’: 169 obs. of 13 variables:


## $ id : num 469 8724 6736 24180 17072 ...
## $ age : num 57.1 75.7 70.8 78.2 54.2 ...
## $ weight_kg : num 64 78 73 60 55 65 77 59 64 72 ...
## $ height_cm : num 156 162 170 148 161 ...
## $ waiting_time : num 18 56 10 14 20 7 26 9 6 10 ...
## $ bmd : num 0.879 0.795 0.907 0.711 0.791 ...

5
## $ bmi : num 0.00265 0.00297 0.00251 0.00274 0.00212 ...
## $ fracture : Factor w/ 2 levels "fracture","no fracture": 2 2 2 2 2 2 2 2 2 2 ...
## $ sex_F : int 1 1 0 1 0 0 0 1 0 0 ...
## $ sex_M : int 0 0 1 0 1 1 1 0 1 1 ...
## $ medication_Anticonvulsant : int 1 0 0 0 0 0 0 0 0 0 ...
## $ medication_Glucocorticoids: int 0 0 0 0 0 0 0 0 1 0 ...
## $ medication_No.medication : int 0 1 1 1 1 1 1 1 0 1 ...
## - attr(*, ".internal.selfref")=<externalptr>

cor(bmd_9_2[,c(2,6,5,9:13)])

## age bmd waiting_time sex_F sex_M


## age 1.00000000 -0.30657644 0.03756105 0.03482870 -0.03482870
## bmd -0.30657644 1.00000000 -0.19206850 -0.26005129 0.26005129
## waiting_time 0.03756105 -0.19206850 1.00000000 0.05906124 -0.05906124
## sex_F 0.03482870 -0.26005129 0.05906124 1.00000000 -1.00000000
## sex_M -0.03482870 0.26005129 -0.05906124 -1.00000000 1.00000000
## medication_Anticonvulsant 0.05955490 -0.11615354 0.07584847 0.08328020 -0.08328020
## medication_Glucocorticoids 0.03843137 -0.03305869 -0.09872779 -0.02668540 0.02668540
## medication_No.medication -0.06757580 0.09490459 0.04397289 -0.02367501 0.02367501
## medication_Anticonvulsant medication_Glucocorticoids
## age 0.05955490 0.03843137
## bmd -0.11615354 -0.03305869
## waiting_time 0.07584847 -0.09872779
## sex_F 0.08328020 -0.02668540
## sex_M -0.08328020 0.02668540
## medication_Anticonvulsant 1.00000000 -0.09649013
## medication_Glucocorticoids -0.09649013 1.00000000
## medication_No.medication -0.48147501 -0.82591262
## medication_No.medication
## age -0.06757580
## bmd 0.09490459
## waiting_time 0.04397289
## sex_F -0.02367501
## sex_M 0.02367501
## medication_Anticonvulsant -0.48147501
## medication_Glucocorticoids -0.82591262
## medication_No.medication 1.00000000

#b.What is the overall fit of the model

full_model = glm(fracture~age + sex + bmi +medication + waiting_time +bmd ,data = bmd_9_, family = binom
full_model

##
## Call: glm(formula = fracture ~ age + sex + bmi + medication + waiting_time +
## bmd, family = binomial, data = bmd_9_)
##
## Coefficients:
## (Intercept) age sexM
## -10.390568 -0.015957 -1.005031
## bmi medicationGlucocorticoids medicationNo medication
## 381.283499 1.212862 -0.617993

6
## waiting_time bmd
## -0.006878 16.979549
##
## Degrees of Freedom: 168 Total (i.e. Null); 161 Residual
## Null Deviance: 205.3
## Residual Deviance: 105 AIC: 121

#c

# d. Which variables are significantly associated with fracture


object = glm(fracture~ + sex + medication + bmd ,data = bmd_9_1, family = binomial)
summary(object)

##
## Call:
## glm(formula = fracture ~ +sex + medication + bmd, family = binomial,
## data = bmd_9_1)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.7172 -0.3439 0.1466 0.4884 2.5164
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -11.5445 2.0357 -5.671 1.42e-08 ***
## sexM -1.0743 0.5201 -2.065 0.0389 *
## medicationGlucocorticoids 1.4650 1.1698 1.252 0.2104
## medicationNo medication -0.5083 0.9399 -0.541 0.5886
## bmd 18.1430 2.9031 6.250 4.12e-10 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 205.27 on 168 degrees of freedom
## Residual deviance: 105.98 on 164 degrees of freedom
## AIC: 115.98
##
## Number of Fisher Scoring iterations: 6

You might also like