temp = c(20, 30, 40, 50, 60, 70, 80, 90, 100, 110) expansion = c(0.11, 0.15, 0.22, 0.28, 0.31, 0.39, 0.43, 0.50, 0.58, 0.63) data <- data.frame(temp, expansion) model = lm(expansion ~ temp, data = data)
2026-03-08
temp = c(20, 30, 40, 50, 60, 70, 80, 90, 100, 110) expansion = c(0.11, 0.15, 0.22, 0.28, 0.31, 0.39, 0.43, 0.50, 0.58, 0.63) data <- data.frame(temp, expansion) model = lm(expansion ~ temp, data = data)
## ## Call: ## lm(formula = expansion ~ temp, data = data) ## ## Residuals: ## Min 1Q Median 3Q Max ## -0.020848 -0.005894 0.003303 0.007591 0.015939 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -0.0189697 0.0102101 -1.858 0.1 ## temp 0.0058303 0.0001437 40.580 1.5e-10 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.01305 on 8 degrees of freedom ## Multiple R-squared: 0.9952, Adjusted R-squared: 0.9946 ## F-statistic: 1647 on 1 and 8 DF, p-value: 1.497e-10
# For scatter plot
ggplot(data, aes(x = temp, y = expansion)) +
geom_point(size = 3) +
geom_smooth(method = "lm", se = FALSE) +
labs(
title = "Rod Expansion vs Temperature",
x = "Temperature (°C)",
y = "Expansion (mm)"
) + theme_minimal()
# For Resi plot
data$residuals <- resid(model)
ggplot(data, aes(x = temp, y = residuals)) +
geom_point(size = 3) +
geom_hline(yintercept = 0, linetype = "dashed") +
labs(
title = "Residual Plot",
x = "Temperature (°C)",
y = "Residual"
) +
theme_minimal()
# For interactive plot
plot_ly(
data = data,
x = ~temp,
y = ~expansion,
type = "scatter",
mode = "markers"
) %>%
layout(
title = "Interactive Scatterplot of Temperature vs Expansion",
xaxis = list(title = "Temperature (°C)"),
yaxis = list(title = "Expansion (mm)")
)
A simple linear regression describes the relationship between two variables. The model is: $$ y = \beta_0 + \beta_1 x + \varepsilon $$ Where: - $\beta_0$ = intercept - $\beta_1$ = slope - $x$ = predictor variable - $y$ = response variable - $\varepsilon$ = random error
The slope of the regression line is calculated using the
least squares formula:
$$
\beta_1 =
\frac{\sum (x_i - \bar{x})(y_i - \bar{y})}
{\sum (x_i - \bar{x})^2}
$$
The intercept is:
$$
\beta_0 = \bar{y} - \beta_1 \bar{x}
$$