library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
fev <- read_csv("fev.csv")
## Rows: 654 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (5): age, fev, height, sex, smoke
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
fev <- fev %>% mutate(smoke = factor(smoke, labels = c("Non-Smoker", "Smoker")), sex = factor(sex, labels = c("Female", "Male")))
fit.mlr <- lm(fev ~ smoke + age + height, data = fev)
summary(fit.mlr)
##
## Call:
## lm(formula = fev ~ smoke + age + height, data = fev)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.50182 -0.26305 -0.01882 0.24989 1.98535
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.616007 0.223883 -20.618 < 2e-16 ***
## smokeSmoker -0.110232 0.060017 -1.837 0.0667 .
## age 0.059741 0.009563 6.247 7.57e-10 ***
## height 0.109095 0.004720 23.115 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4189 on 650 degrees of freedom
## Multiple R-squared: 0.7676, Adjusted R-squared: 0.7665
## F-statistic: 715.7 on 3 and 650 DF, p-value: < 2.2e-16
#OR
#
2.2.1: -4.616007 + (-0.110232 X smoke) + (0.059741 x age) + (0.109095 x height)
2.1.2:
mydata <- data.frame(smoke = "Non-Smoker", age = 8, height = 60)
predict(fit.mlr, newdata = mydata)
## 1
## 2.407606
Using the predict function, the FEV prediction for a non-smoking, 5-foot tall eight year old is 2.407606.
2.1.3: a) The smoke coefficient estimate was -0.110232.
This smoke coefficient tells us that for people of the same age and height, if one is a smoker versus a non-smoker, the smoker will have on average an fev that is -0.110232 less than that of the non-smoker.
The smoke coefficient from Lab 2 was 0.71072, this is represents the change in fev for a smoker compared to a non-smoker and nothing else.
The key difference between these two models is the implementation of weights in regards to age and height. This will adjust our output to account for age and height rather than just smoking versus not smoking.
Section 3: Plots for MLR
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
fev %>% select(fev, age, height, smoke) %>% ggpairs()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
3.2:
fit.y <- lm(fev ~ smoke + age + height, data = fev)
fev$residuals_y <- residuals(fit.y)
fit.x <- lm(height ~ smoke + age, data = fev)
fev$residuals_x <- residuals(fit.x)
ggplot(fev, aes(x = residuals_x, y = residuals_y)) + geom_point()
fit.slr2 <- lm(residuals_y ~ residuals_x, data = fev)
summary(fit.slr2)
##
## Call:
## lm(formula = residuals_y ~ residuals_x, data = fev)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.50182 -0.26305 -0.01882 0.24989 1.98535
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.569e-17 1.636e-02 0 1
## residuals_x -3.308e-17 4.712e-03 0 1
##
## Residual standard error: 0.4183 on 652 degrees of freedom
## Multiple R-squared: 6.486e-32, Adjusted R-squared: -0.001534
## F-statistic: 4.229e-29 on 1 and 652 DF, p-value: 1
#I dont think this is right
3.3:
fev <- fev %>% mutate(Residuals=fit.mlr$residuals, Fitted=fit.mlr$fitted.values)
residual_hist <- fev %>% ggplot(aes(x = Residuals)) + geom_histogram() +
theme_bw() +
labs(title="Histogram of Residuals")
residual_hist
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
qqplot <- fev %>% ggplot() + stat_qq(aes(sample=Residuals)) +
theme_bw() +
geom_abline(slope=sd(fev$Residuals),intercept=0, col="red", lty=2) +
labs(title="Normal Quantile (QQ) Plot", x="Theoretical Quantiles", y="Residuals")
qqplot
scatter <- fev %>% ggplot(aes(x = Fitted, y = Residuals)) + geom_point() +
theme_bw() +
labs(title="Residuals vs. Fitted Values", x="Fitted", y = "Residuals")
scatter
3.4: 1. There seems to be a moderate, positive, linear correlation between FEV and both age and height. However, FEV in regards to smoking versus non smoking seems to be a right-skewed histogram. 2. The adjusted relationship between FEV and height shows a much more random scatter than the unadjusted relationship, indicating that FEV and height by themselves do not have much association. 3. Since the histogram of residuals resembles a normal distribution I believe the assumption of normality and equal variance is plausible.
4 Specifying flexible model terms in R
4.1:
fit <- lm(fev ~ height + I(height^2), data = fev)
4.2: You could use an if-else statement, with a boolean operator in the if statement to check the if the height is greater than 65 inches.
4.3:
fit <- lm(fev ~ age + smoke + height + smoke*age, data = fev)
4.4:
fev <- fev %>% mutate(agegp = cut(age, seq(0,25,5)))
glimpse(fev)
## Rows: 654
## Columns: 10
## $ age <dbl> 9, 8, 7, 9, 9, 8, 6, 6, 8, 9, 6, 8, 8, 8, 8, 7, 5, 6, 9, 9…
## $ fev <dbl> 1.708, 1.724, 1.720, 1.558, 1.895, 2.336, 1.919, 1.415, 1.…
## $ height <dbl> 57.0, 67.5, 54.5, 53.0, 57.0, 61.0, 58.0, 56.0, 58.5, 60.0…
## $ sex <fct> Female, Female, Female, Male, Male, Female, Female, Female…
## $ smoke <fct> Non-Smoker, Non-Smoker, Non-Smoker, Non-Smoker, Non-Smoker…
## $ residuals_y <dbl> -0.432062606, -1.501816318, -0.027843663, -0.145683650, -0…
## $ residuals_x <dbl> -2.7751255, 9.2910685, -2.1427375, -6.7751255, -2.7751255,…
## $ Residuals <dbl> -0.432062606, -1.501816318, -0.027843663, -0.145683650, -0…
## $ Fitted <dbl> 2.1400626, 3.2258163, 1.7478437, 1.7036836, 2.1400626, 2.5…
## $ agegp <fct> "(5,10]", "(5,10]", "(5,10]", "(5,10]", "(5,10]", "(5,10]"…
str(fev$agegp)
## Factor w/ 5 levels "(0,5]","(5,10]",..: 2 2 2 2 2 2 2 2 2 2 ...
fit.newage <- lm(fev ~ smoke + as.factor(agegp) + height, data = fev)
#plot(fit.newage)
4.4.1
lm(Y ~ size) matches with Expected Y = β0 + β1 × size
lm(Y ~ as.factor(size)) matches with Expected Y = β0 + β1 × I(medium) + β2 × I(large)
#fev <- fev %>% mutate(olderadult = ifelse(age > 60, "Yes", "No"))
4.5 Questions
I would describe β2 as the average change in FEV for a persons who is taller than 65 inches. A coefficient that only gets turned on if when a person is taller than 65 inches.
I would interpret the coefficient term as the average change in FEV with respect to age for a person that smokes.
There would be two parallel lines as both of these variables when compared to each other may very well have little impact on our estimate of FEV.
The cut function is only going up to the age 25, essentially children and young adults are our reference category. We are missing the middle aged and older adults groups.
Each slope coefficient still carries the same meaning except for the ones correlated with age as they will now be indicator random variables with in this case 5 indicators.