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.
Average_birth_weight = 120.07 - 1.93*parity
The average birth rate decreases by 1.93 (the value of slope) if a child is NOT first born.
Predicted birth weight of first borns: Average_birth_weight = 120.07 - 1.93*0 = 120.07
Predicted birth weight of others: Average_birth_weight = 120.07 - 1.93*1 = 118.14
Let’s state the hypothesis:
Null hypothesis: Slope equals to 0. There is no statistically significant relationship between the average birth weight and parity
Alternative hypothesis: Slope DOESN’T equal to 0. There is statistically significant relationship between the average birth weight and parity.
Since p value equals to 0.1052 (which is less that significance level of 5%) we have to reject the null hypothesis. Hence, Slope DOESN’T equal to 0. There is statistically significant relationship between the average birth weight and parity.
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.
Average_number_of_days_absent = 18.93 - 9.11eth + 3.1sex + 2.15lrn
The slope of -9.11 indicates that average number of days is decreased by 9.11 if student is NOT aboriginal.
The slope of 3.1 indicates that average number of days is increased by 3.1 if student is male.
The slope of 2.15 indicates that average number of days is increased by 2.15 if student is slow learner.
Average_number_of_days_absent <- 18.93 - 9.11*0 + 3.1*1 + 2.15*1
residual <- 2 - Average_number_of_days_absent
residual
## [1] -22.18
variance_residuals <- 240.57
variance_outcome <- 264.17
num_observations <- 146
R2 <- 1 - variance_residuals/variance_outcome
R2
## [1] 0.08933641
R2.adj <- 1 - (variance_residuals/variance_outcome)*((num_observations-1)/(num_observations-3-1))
R2.adj
## [1] 0.07009704
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?
The model #4 (with no learner) has the highest adjusted R2. The greater the adjusted R2 the better data fits a model. So, the variable learner should be removed.
The learner status should be removed from the model first, since we get a better adjusted R2
On January 28, 1986, a routine launch was anticipated for the Challenger space shuttle. Seventy-three seconds into the flight, disaster happened: the shuttle broke apart, killing all seven crew members on board. An investigation into the cause of the disaster focused on a critical seal called an O-ring, and it is believed that damage to these O-rings during a shuttle launch may be related to the ambient temperature during the launch. The table below summarizes observational data on O-rings for 23 shuttle missions, where the mission order is based on the temperature at the time of the launch. Temp gives the temperature in Fahrenheit, Damaged represents the number of damaged O-rings, and Undamaged represents the number of O-rings that were not damaged.
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(1,5,5,5,6,6,6,6,6,6,5,6,5,6,6,6,6,5,6,6,6,6,6)
data <- data.frame(temperature,damaged,undamaged)
head(data)
## temperature damaged undamaged
## 1 53 5 1
## 2 57 1 5
## 3 58 1 5
## 4 63 1 5
## 5 66 0 6
## 6 67 0 6
data <- data %>% gather(condition,value,damaged:undamaged)
head(data)
## temperature condition value
## 1 53 damaged 5
## 2 57 damaged 1
## 3 58 damaged 1
## 4 63 damaged 1
## 5 66 damaged 0
## 6 67 damaged 0
library(ggplot2)
plot <- ggplot(data, aes(x=value, y=temperature, group=condition)) + geom_point(aes(color=condition))
plot
The plot shows that as tempruter decreases the number of damaged cases increasesns as temprutere decreases the number of undamaged cases decreases.
In order to analyze the data the logistic regression should be used since response variable “damaged O-rings” is binary variable (that equals either to “Damadged”or “Undamaged”) and explonatory variable “tempriture” is continues variable.
The variable “tempriture” is statistically sagnificant since its p-value equals to 0 (it’s smaller than the sagnificance level of 5%). The intersept of 11.6630 indicates that if tempredure is 0 than the natural logarithm of odds ration is 11.6630. The slope of -0.2162 indicates that each single increase in tempreture decreases the natural logarithm of odds ratio by 0.2162.
ln(p/1-p) = 11.6630 - 0.02162*tempreture
Since the variable “tempriture” is statistically sagnificant the chages in tepreture affect the chages in probability of success (getting chuttle undamadged). So that, I would say that concerns regarding O-rings are justified.
ln(p/1-p) = 11.6630 - 0.2162*temperature
Let’s find p.
find_p <- function(temperature)
{
logit_p <- 11.6630 -0.2162*temperature
p <- exp(1)^logit_p/(1+exp(1)^logit_p)
return (p)
}
find_p(51)
## [1] 0.6540297
find_p(53)
## [1] 0.5509228
find_p(55)
## [1] 0.4432456
temperature <- c()
probability <- c()
for (i in 51:81){
temperature[i] <- i
probability[i]<- find_p(i)
}
data_prob <- data.frame(temperature,probability )
g <- ggplot(data_prob, aes(x=temperature,y=probability)) + geom_line() + ggtitle("Change in probability based on change temperature")
g
## Warning: Removed 50 rows containing missing values (geom_path).
The graph shows that the probability increases as temperature decreases.
The assumptions for the logistic regression are:
Logistic regression typically requires a large sample size. It requires a minimum of 10 cases with the least frequent outcome for each independent variable in the model. Since a model has one independent variables and the expected probability of the least frequent outcome is .10, then the minimum sample should include 100 (10*1 / .10) observation. Shuttle missions data set is not big enough.
Logistic regression requires the observations to be independent of each other. I assume that observations of the data set are independent.
Logistic regression requires there to be little or no multicollinearity among the independent variables. This means that the independent variables should not be too highly correlated with each other.
This condition is not applicable to shuttle mission data set because the response variable is not a numeric type.
library(dplyr)
data <- data %>% mutate(temperature = temperature*log(temperature),condition=as.factor(condition))
head(data)
## temperature condition value
## 1 210.4255 damaged 5
## 2 230.4539 damaged 1
## 3 235.5057 damaged 1
## 4 261.0175 damaged 1
## 5 276.5172 damaged 0
## 6 281.7144 damaged 0
logistic_model <- glm(formula = condition ~ temperature, family = binomial(link = "logit"),data = data[1:2])
summary(logistic_model)
##
## Call:
## glm(formula = condition ~ temperature, family = binomial(link = "logit"),
## data = data[1:2])
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.177 -1.177 0.000 1.177 1.177
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.323e-14 2.442e+00 0 1
## temperature 4.555e-17 8.205e-03 0 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 63.77 on 45 degrees of freedom
## Residual deviance: 63.77 on 44 degrees of freedom
## AIC: 67.77
##
## Number of Fisher Scoring iterations: 2
I tested linearity in the logit by running Box-Tidwell Transformation Test. I added to the logistic model interaction terms which are the crossproduct of each independent times its natural logarithm ( (X)ln(X)] ). If these terms are significant, then there is non-linearity in the logit.
I stated the following hypothesis:
The null hypothesis: Odds ration is linear with an independent variable. No transformation is needed.
The alternative hypothesis: Odds ration is not linear with an independent variable. Transformation is needed.
The null hypothesis is not rejected as p-values is greater than significance level of 5%. Hence, transformation is not needed for the variable “total_acc”.