Module #7 Assignment
1.
# Define the data
x <- c(16, 17, 13, 18, 12, 14, 19, 11, 11, 10)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# Create a linear regression model
model <- lm(y ~ x)
# Summary of the model to get coefficients
summary(model)
1.1
The relationship model is y = a + bx, where y is the dependent variable (response), x is the independent variable (predictor), a is the intercept, and b is the coefficient of x.
1.2
intercept <- coef(model)[1]
slope <- coef(model)[2]
2.
# Data
visit <- data.frame(
discharge = c(3.600, 1.800, 3.333, 2.283, 4.533, 2.883),
waiting = c(79, 54, 74, 62, 85, 55)
)
# Fit a simple linear regression model
model2 <- lm(discharge ~ waiting, data = visit)
2.1
The relationship model is discharge = a + b * waiting
where a is the intercept, and b is the slope.
2.2
intercept2 <- coef(model2)[1]
slope2 <- coef(model2)[2]
intercept2 # Value of a
slope2 # Value of b
2.3
# Predict discharge when waiting time is 80 minutes
predicted_discharge <- predict(model2, newdata = data.frame(waiting = 80))
predicted_discharge
3.
# Load the mtcars data and select the variables
input <- mtcars[, c("mpg", "disp", "hp", "wt")]
# Fit a multiple regression model
model3 <- lm(mpg ~ disp + hp + wt, data = input)
3.1
The relationship model is mpg = a + b1 * disp + b2 * hp + b3 * wt, where a is the intercept, and b1, b2, and b3 are the coefficients for disp, hp, and wt respectively.
# Get coefficients
intercept3 <- coef(model3)[1]
coef_disp <- coef(model3)[2]
coef_hp <- coef(model3)[3]
coef_wt <- coef(model3)[4]
intercept3 # Value of a
coef_disp # Coefficient for disp
coef_hp # Coefficient for hp
coef_wt # Coefficient for wt
4.
# Install and load the ISwR package if not already installed
if (!require(ISwR)) {
install.packages("ISwR")
library(ISwR)
}
# Load the rmr data
data(rmr)
# Plot metabolic rate versus body weight
plot(rmr$metabolic.rate ~ rmr$body.weight)
# Fit a linear regression model
model4 <- lm(metabolic.rate ~ body.weight, data = rmr)
# Predict metabolic rate for a body weight of 70 kg
predicted_metabolic_rate <- predict(model4, newdata = data.frame(body.weight = 70))
predicted_metabolic_rate
Comments
Post a Comment