- Simple linear regression is used to find a straight line that fits a set of data points
- Regression must include an independent variable and a dependent variable
2024-03-17
flowersThe dataset I will be working with to illustrate simple linear regression has information regarding flowers
head(flowers)
## treat nitrogen block height weight leafarea shootarea flowers ## 1 tip medium 1 7.5 7.62 11.7 31.9 1 ## 2 tip medium 1 10.7 12.14 14.1 46.0 10 ## 3 tip medium 1 11.2 12.76 7.1 66.7 10 ## 4 tip medium 1 10.4 8.78 11.9 20.3 1 ## 5 tip medium 1 10.4 13.58 14.5 26.9 4 ## 6 tip medium 1 9.8 10.08 12.2 72.7 9
x <- flowers$weight y <- flowers$shootarea ggplot(flowers, aes(x = x, y = y)) + geom_point() + # Add points labs(x = "Weight of Flower", y = "Shoot Area", title = "Flower Weight vs. Shoot Area")
x <- flowers$weight y <- flowers$shootarea ggplot(flowers, aes(x = x, y = y)) + geom_boxplot() + labs(title = "Boxplot") # Add axis labels and title
## Warning: Continuous x aesthetic ## ℹ did you forget `aes(group = ...)`?
model <- lm(flowers$shootarea~flowers$weight) #simple linear regression model summary(model)
## ## Call: ## lm(formula = flowers$shootarea ~ flowers$weight) ## ## Residuals: ## Min 1Q Median 3Q Max ## -78.352 -29.694 -3.447 28.143 68.884 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -18.4346 12.2048 -1.510 0.134 ## flowers$weight 8.0807 0.9546 8.465 3.34e-13 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 37.1 on 94 degrees of freedom ## Multiple R-squared: 0.4326, Adjusted R-squared: 0.4265 ## F-statistic: 71.66 on 1 and 94 DF, p-value: 3.342e-13
Here is a plot with the data and the regression equation line:
x <- flowers$weight
y <- flowers$shootarea
xax <- list(title = "Flower Weight",
titlefont = list(family = "Modern Computer Roman"))
yax <- list(title = "Shoot Area",
titlefont = list(family = "Modern Computer Roman"),
range = c(0, 80))
fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers", name = "data",
width = 800, height = 430) %>%
add_lines(x = x, y = fitted(model), name = "fitted") %>%
layout(xaxis = xax, yaxis = yax) %>%
layout(margin = list(l = 150, r = 50, b = 20, t = 40))
config(fig, displaylogo = T)