library(ggplot2)
library('DATA606')          # Load the package
##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 3rd Edition. You can read this by typing
## vignette('os3') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
library(knitr)

# 8.2:- Baby weights, Part II. Exercise 8.1 introduces a data set on birth weight of babies. Another variable we consider is parity, which is 0 if the child is the first born, and 1 otherwise. The summary table below shows the results of a linear regression model for predicting the average birth weight of babies, measured in ounces, from parity.

## (a) Write the equation of the regression line.

Regression line $$\hat{y} = \beta_0 + \beta_1{x}$$

Intercept $$\beta_0 = 120.07$$, Slope $$\beta_1 = -1.93$$

Substituting values, $$\hat{y} = 120.07 -1.93x$$

## (b) Interpret the slope in this context, and calculate the predicted birth weight of first borns and others.

Using above data, parity can be identified as a categorical value. It represents a value of 0 for first born and 1 for all others. Weight of first borns is 1.93 ounces higher than others.

When child is first born parity $$x = 0, \hat{y}_{first\ born\ weight} = \beta_0 + \beta_1{x}$$

$$\hat{y}_{first\ born\ weight} = 120.07 -1.93 * 0$$

Predicted birth weight of first borns is 120.07 ounces

When child is not first born parity $$x = 1, \hat{y}_{non\ first\ born\ weight} = \beta_0 + \beta_1{x}$$

$$\hat{y}_{non\ first\ born\ weight} = 120.07 -1.93 * 1 = 118.14$$

Predicted birth weight of non-first borns is 118.14 ounces

## (c) Is there a statistically significant relationship between the average birth weight and parity?

Null hypothesis: There is no difference in birth weights $$H_0: = \beta_0 = 0$$. Alternative hypothesis: There is difference in weights $$H_A: \beta_0 \ne 0$$

From the table $$t-value = -1.62$$ and the $$p-value = 0.1052$$.

Since the p-value is greater than $$\alpha = 0.05$$, we accept the $$H_0$$. There is no difference in birth weights first born or not. Solving regression equation results otherwise. Due to the differences in results, it can be concluded that regression equation is statistically significant predictor and not a practically significant predictor.

# 8.4:- Absenteeism, Part I. Researchers interested in the relationship between absenteeism from school and certain demographic characteristics of children collected data from 146 randomly sampled students in rural New South Wales, Australia, in a particular school year. Below are three observations from this data set.

## (a) Write the equation of the regression line.

Regression line $$\hat{y} = \beta_0 + \beta_1{x_1} + \beta_2{x_2} + \beta_3{x_3}$$

Intercept $$\beta_0 = 18.93$$, Slopes $$\beta_1 = -9.11, \beta_1 = 3.10, \beta_1 = 2.15$$

Intercept $$x_1 = eth,\ x_2 = sex,\ x_3 = lrn$$

Substituting values, $$\hat{y} = 18.93 -9.11eth + 3.10sex + 2.15lrn$$

## (b) Interpret each one of the slopes in this context.

The slope values are interpreted as a measure of change in units of y for a unit increase or decrease in a particular X predictor variable, given that the other variables are held constant.

Interpretation using slope of ethnic background variable, predicted absenteeism is 9.11 days higher among children belonging to aboriginal descent compared to non-aboriginal descent, when child gender and learning status are held constant.

Using slope of gender(sex) variable, predicted absenteeism is 3.10 days higher among male children when ethnic background and learning status of child are held constant.

Using slope of learner status variable, predicted absenteeism is 2.15 days higher among children classified as slow learner when ethnic background and gender of child are held constant.

## (c) Calculate the residual for the first observation in the data set: a student who is aboriginal, male, a slow learner, and missed 2 days of school.

Residual for the first observation in the data set with predictor variables, eth = 0, sex = 1, lrn = 1 and observed absenteeism $$y_observed = 2$$ days

$$\hat{y}_{predicted} = 18.93 -9.11eth + 3.10sex + 2.15lrn$$

$$\hat{y}_{predicted} = 18.93 -9.11 * 0 + 3.10 * 1 + 2.15 * 1$$

$$\hat{y}_{predicted} = 24.18$$

Residual $$e_i = y_i - \hat{y} = 2 - 24.18 = -22.18$$

## (d) The variance of the residuals is 240.57, and the variance of the number of absent days for all students in the data set is 264.17. Calculate the R2 and the adjusted R2. Note that there are 146 observations in the data set.

Variance of the residuals $$Var(e_i) = 240.57$$, Variance of the number of absent days for all students(outcomes) $$Var(y_i) = 264.17$$

To determine the amount of variability in the response that was explained by the model $$R^2 = 1 - \frac{Var(e_i)} {Var(y_i)}$$

$$R^2 = 1 - \frac{240.57} {264.17} = 0.0893$$

Total number of observations $$n = 146$$, number of preditor variables $$k = 3$$, Adjusted $${R^2}_{adj} = 1 - {\frac{Var(e_i)} {Var(y_i)}} * {\frac{n -1} {n-k-1}}$$

Adjusted $${R^2}_{adj} = 1 - {\frac{240.57} {264.17}} * {\frac{146 -1} {146-3-1}} = 0.0701$$

# 8.8:- Absenteeism, Part II. Exercise 8.4 considers a model that predicts the number of days absent using three predictors: ethnic background (eth), gender (sex), and learner status (lrn). The table below shows the adjusted R-squared for the model as well as adjusted R-squared values for all models we evaluate in the first step of the backwards elimination process. Which, if any, variable should be removed from the model first?

Adjusted $$R^2$$ describes the strength of a model fit, and it is a useful tool for evaluating which predictors improve the accuracy in predicting future outcomes. Greater the value of Adjusted $$R^2$$, better the strength of a model fit. Above table shows model without learner status predictor variable has greater Adjusted $$R^2$$. Hence, learner status predictor variable should be removed to improve the accuracy in predicting future outcomes.

# Appendix

library(ggplot2)
library('DATA606')          # Load the package
library(knitr)
library(knitr)
orig.data <- data.frame(NA,NA,NA, stringsAsFactors = F)
orig.data <- rbind(orig.data, c("Below $66^\\circ F$", 8, round((8/11)*100,2)), stringsAsFactors = F)
orig.data <- rbind(orig.data, c("$66^\\circ F$ and Above", 3, round((3/11)*100,2)), stringsAsFactors = F)
orig.data<-na.omit(orig.data)
rownames(orig.data) <- NULL
kable(orig.data, digits = 2, col.names = c("Temperature", "Damages", "Percentage(%)"), format='pandoc', caption = "O-ring damages based on temperature")
p <- function(temp)
{
phat <- exp(11.6630 - 0.2162 * temp) / (1 + exp(11.6630 - 0.2162 * temp))

return (round(phat,3))
}
p(51)
p(53)
p(55)
prob <- data.frame(shuttle=seq(1:23),
temperature=c(53,57,58,63,66,67,67,67,68,69,70,70,70,70,72,73,75,75,76,76,78,79,81),
damaged=c(5,1,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0),
undamaged=c(c(1,5,5,5,6,6,6,6,6,6,5,6,5,6,6,6,6,5,6,6,6,6,6)))
prob$rating <- prob$damaged / (prob$damaged + prob$undamaged)
ggplot(prob,aes(x=temperature,y=damaged)) + geom_point() +  stat_smooth(method = 'glm', family ='binomial')