**Sign Test & Wilcoxon Signed-Rank Test on Sleep Data:-** ```{r} data(sleep) ``` ```{r} sleep ``` ```{r} drug1 <- sleep$extra[sleep$group == 1] drug2 <- sleep$extra[sleep$group == 2] ``` ```{r} differences <- drug2 - drug1 ``` ```{r} d <- differences[differences != 0] ``` ```{r} binom.test( sum(d > 0), length(d), p = 0.5 ) ``` ```{r} wilcox.test( drug2, drug1, paired = TRUE, alternative = "two.sided", exact = FALSE ) ``` ```{r} wilcox.test( differences, mu = 0, alternative = "two.sided", exact = FALSE ) ``` **Wilcoxon Rank-Sum Test or Mann-Whitney U-Test on Air Quality Data:-** ```{r} data(airquality) ``` ```{r} airquality ``` ```{r} may <- na.omit(airquality$Ozone[airquality$Month == 5]) august <- na.omit(airquality$Ozone[airquality$Month == 8]) ``` ```{r} may ``` ```{r} wilcox.test( may, august, alternative = "two.sided", exact = FALSE ) ``` **Linear Regression & Multiple Linear Regression on Dental Data:-** ```{r} data(ToothGrowth) ``` ```{r} ToothGrowth ``` ```{r} unique(ToothGrowth$supp) ``` ```{r} linear_model <- lm(len ~ dose, data = ToothGrowth) summary(linear_model) ``` ```{r} plot(ToothGrowth$dose, ToothGrowth$len, xlab = "Supplement Dosage", ylab = "Tooth length", main = "Tooth length vs dose") abline(linear_model) ``` ```{r} multiple_model <- lm(len ~ dose + supp, data = ToothGrowth) summary(multiple_model) ``` ```{r} interaction_model <- lm(len ~ dose * supp, data = ToothGrowth) summary(interaction_model) ``` ```{r} par(mfrow = c(2, 2)) plot(interaction_model) par(mfrow = c(1, 1)) ``` ```{r} # 2. Create a grid with a fine sequence of dose values for smooth regression lines newdata_0 <- expand.grid( dose = seq(0.5, 2, by = 0.05), supp = levels(ToothGrowth$supp) ) # 3. Predict values from the continuous model newdata_0$predicted <- predict(interaction_model, newdata = newdata_0) # 4. Plot the predicted linear trend lines ggplot(newdata_0, aes(x = dose, y = predicted, color = supp, group = supp)) + geom_line(linewidth = 1) + labs( x = "Dose (mg/day)", y = "Predicted Tooth Length", color = "Supplement" ) + theme_pubr() ``` **Linear Regression with Interactions:-** ```{r} factor_model <- lm(len ~ factor(dose) * supp, data = ToothGrowth) summary(factor_model) ``` ```{r} par(mfrow = c(2, 2)) plot(factor_model) par(mfrow = c(1, 1)) ``` ```{r} library(ggplot2) library(ggpubr) ``` ```{r} newdata <- expand.grid( dose = c(0.5, 1, 2), supp = levels(ToothGrowth$supp) ) newdata$predicted <- predict(factor_model, newdata = newdata) ggplot(newdata, aes(x = dose, y = predicted, color = supp, group = supp)) + geom_line(linewidth = 1) + geom_point(size = 3) + labs( x = "Dose", y = "Predicted tooth length", color = "Supplement" ) + theme_pubr() ``` **Variance Inflation Factor:-** ```{r} library(car) ``` ```{r} data(mtcars) ``` ```{r} mtcars ``` ```{r} cars_model_1 <- lm(mpg ~ hp + wt, data = mtcars) ``` ```{r} vif(cars_model_1) ``` ```{r} cars_model_2 <- lm(mpg ~ disp + hp + wt + qsec, data = mtcars) ``` ```{r} vif(cars_model_2) ``` **When is Linear Regression Inadequate? Introduction to GLMs** ```{r} set.seed(123) n <- 200 x <- runif(n, 0, 10) y <- 10 + 2*x + rnorm(n, mean = 0, sd = 3) normal_data <- data.frame(x, y) ``` ```{r} normal_data ``` ```{r} normal_model <- lm(y ~ x, data = normal_data) summary(normal_model) ``` ```{r} plot( normal_data$x, normal_data$y, pch = 19, xlab = "X", ylab = "Y", main = "Normally Distributed Continuous Data" ) abline(normal_model, lwd = 2) ``` ```{r} par(mfrow = c(2, 2)) plot(normal_model) par(mfrow = c(1, 1)) ``` ```{r} hist( residuals(normal_model), main = "Distribution of Residuals", xlab = "Residuals" ) ``` ```{r} hist( normal_data$y, main = "Distribution of the Response Variable", xlab = "Response Variable" ) ``` ```{r} qqnorm(residuals(normal_model)) qqline(residuals(normal_model)) ``` ```{r} set.seed(123) n <- 200 x <- runif(n, 0, 10) mu <- exp(1 + 0.3*x) y_count <- rnbinom( n, size = 2, mu = mu ) nb_data <- data.frame(x, y_count) ``` ```{r} nb_data ``` ```{r} hist( nb_data$y, main = "Distribution of the Response Variable", xlab = "Response Variable" ) ``` ```{r} mean(nb_data$y_count) var(nb_data$y_count) ``` ```{r} nb_lm <- lm(y_count ~ x, data = nb_data) summary(nb_lm) ``` ```{r} plot( nb_data$x, nb_data$y_count, pch = 19, xlab = "X", ylab = "Count", main = "Negative Binomial Counts with Linear Regression" ) abline(nb_lm, lwd = 2) ``` ```{r} par(mfrow = c(2, 2)) plot(nb_lm) par(mfrow = c(1, 1)) ``` ```{r} library(MASS) nb_model <- glm.nb( y_count ~ x, data = nb_data ) summary(nb_model) ``` ```{r} par(mfrow = c(2, 2)) plot(nb_model) par(mfrow = c(1, 1)) ```