In this project, we will explore predictive analytics using the babies data set provided. This data is collected to understand different factors effecting baby weights at birth. Below are the explanations of the variables:
Count: number of observation bwt: Baby weights in ounces gestation: Length of pregnancy in days parity: = 0 if the baby is first born, =1 otherwise height: Mother’s height in inches weight: Mother’s pregnancy weight in pounds smoke: =1 if mother is smoker, = 0 Nonsmoker age: Mother’s age in years
Read this worksheet carefully and follow the instructions to complete the tasks and answer any questions. Knit your final worksheet and submit your work as an HTML or PDF or Word document in Sakai.
If you are submitting as a team project only one submission from one of the team members will be sufficient but in this case you need to input the names of the team members in the above Title section.
#Read File and check the first 6 observations
mydata = read.csv(file = "babies.csv")
head(mydata)
## Count bwt gestation parity height weight smoke age
## 1 1 120 284 0 62 100 0 27
## 2 2 113 282 0 64 135 0 33
## 3 3 128 279 0 64 115 1 28
## 4 4 108 282 0 67 125 1 23
## 5 5 136 286 0 62 93 0 25
## 6 6 138 244 0 62 178 0 33
nrow(mydata)
## [1] 1174
"The relationship between maternal age and weight, along with babye weight is expected to show positive correlations. This suggest thatc older and heavier mothers are likely to have babies with higher birth weights, potentially due to improved maternal health and nutrition. This highlights how maternal factors and gestational conditions interact to influence baby weights at birth"
## [1] "The relationship between maternal age and weight, along with babye weight is expected to show positive correlations. This suggest thatc older and heavier mothers are likely to have babies with higher birth weights, potentially due to improved maternal health and nutrition. This highlights how maternal factors and gestational conditions interact to influence baby weights at birth"
All_reg <- lm(bwt ~ gestation + parity + height + weight + smoke + age, data = mydata)
summary(All_reg)
##
## Call:
## lm(formula = bwt ~ gestation + parity + height + weight + smoke +
## age, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -57.613 -10.189 -0.135 9.683 51.713
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -80.41085 14.34657 -5.605 2.60e-08 ***
## gestation 0.44398 0.02910 15.258 < 2e-16 ***
## parity -3.32720 1.12895 -2.947 0.00327 **
## height 1.15402 0.20502 5.629 2.27e-08 ***
## weight 0.05017 0.02524 1.987 0.04711 *
## smoke -8.40073 0.95382 -8.807 < 2e-16 ***
## age -0.00895 0.08582 -0.104 0.91696
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.83 on 1167 degrees of freedom
## Multiple R-squared: 0.258, Adjusted R-squared: 0.2541
## F-statistic: 67.61 on 6 and 1167 DF, p-value: < 2.2e-16
#Multiple Linear Regression Model
model <- lm(bwt ~ . - Count, data = mydata)
#Summary of the model
summary(model)
##
## Call:
## lm(formula = bwt ~ . - Count, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -57.613 -10.189 -0.135 9.683 51.713
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -80.41085 14.34657 -5.605 2.60e-08 ***
## gestation 0.44398 0.02910 15.258 < 2e-16 ***
## parity -3.32720 1.12895 -2.947 0.00327 **
## height 1.15402 0.20502 5.629 2.27e-08 ***
## weight 0.05017 0.02524 1.987 0.04711 *
## smoke -8.40073 0.95382 -8.807 < 2e-16 ***
## age -0.00895 0.08582 -0.104 0.91696
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.83 on 1167 degrees of freedom
## Multiple R-squared: 0.258, Adjusted R-squared: 0.2541
## F-statistic: 67.61 on 6 and 1167 DF, p-value: < 2.2e-16
intercept<-coef(model)[1]
gestation_coef<-coef(model)[“gestation”]
weight_coef<-(model)[“weight”]
smoke_coef<-coef(model)[“smoke”]
r_squared<-summary(model)$r.squared
coefficients<-coef(model)values<-summary(model)$coefficients[,4]significance<-ifelse(values<0.05,“Significant”, “Not Significant”)results<-data.frame(Variable=names(coefficients),Coefficient=coefficients,P_Value=values,Signidicance=significance)print(results) #### Do you think the model has insignificant variables, if so which variable(s) is/are insignificant? Yes, the “age” variable is not significant. ####Is removing insignificant variables improving your model’s predictivity? If removing an insignificant variable results in a significant increase in r-squared, it could enhance predictability. However, it might not be justified to remove the variable if the improvement is minimal or absent.
#Multiple Linear Regression Model
model <- lm(bwt ~ . - Count, data = mydata)
#Summary of Multiple Linear Regression Model
summary(model)
##
## Call:
## lm(formula = bwt ~ . - Count, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -57.613 -10.189 -0.135 9.683 51.713
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -80.41085 14.34657 -5.605 2.60e-08 ***
## gestation 0.44398 0.02910 15.258 < 2e-16 ***
## parity -3.32720 1.12895 -2.947 0.00327 **
## height 1.15402 0.20502 5.629 2.27e-08 ***
## weight 0.05017 0.02524 1.987 0.04711 *
## smoke -8.40073 0.95382 -8.807 < 2e-16 ***
## age -0.00895 0.08582 -0.104 0.91696
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.83 on 1167 degrees of freedom
## Multiple R-squared: 0.258, Adjusted R-squared: 0.2541
## F-statistic: 67.61 on 6 and 1167 DF, p-value: < 2.2e-16
###Task 4: Estimating baby weight (0.25 points)
Use your best predicting model and full model to estimated a first born babies weight, whose mother is 25 years old, newer smoked, the gestation was 286 days, mother’s height is 62 inches and mother’s weight is 93 pounds
# bwt predicted
newdata <- data.frame(gestation = 286, parity = 0, height = 62, weight = 93, smoke = 0, age = 25)
best_coeffs <- c(intercept = -80.41085, gestation = 0.44398, parity = -3.32720, height = 1.15402, weight = 0.05017, smoke = -8.40073, age = -0.00895)
predicted_bwt<-sum(best_coeffs * c(1, as.numeric(newdata)))
cat("Predict birth weight:", round(predicted_bwt, 2), "grams\n")
## Predict birth weight: 122.56 grams