Using the below sample data, write R code to create a linear regression model predicting weight based on height. Then predict the weight of someone bsaed on a height of 165. Sample data: height <- c(150, 160, 170, 180, 190) weight <- c(50, 60, 65, 75, 80)
Regression analysis helps understand relationships between variables. In R, you can create simple linear regression models to predict a dependent variable based on one or more independent variables.
# Sample data
height <- c(150, 160, 170, 180, 190)
weight <- c(50, 60, 65, 75, 80)
# Create linear model
model <- lm(weight ~ height)
# View model summary
summary(model)
lm
): This function fits a straight line through the data that best explains the relationship between the independent variable (height
) and the dependent variable (weight
).summary(model)
shows the estimated equation, statistical significance of the relationship, and how well the model fits the data (R-squared value).# Predict weight for a new height
predict(model, data.frame(height = 175))
predict()
: Once the line of best fit is created, you can use it to estimate new values.height = 175
, the model plugs 175 into the regression equation (slope × 175 + intercept) to predict the expected weight.Regression is a powerful tool for exploring relationships and making predictions based on data trends.