What, Why Model
- Predicts numeric \(y\) from numeric \(x\)
- Interprets the slope as expected change in \(y\) per +1 in \(x\)
What, Why Model
\[ y_i = \beta_0 + \beta_1 x_i + \varepsilon_i, \qquad \varepsilon_i^{\text{iid}} \sim \mathcal{N}(0,\sigma^2). \]
\[ \hat{\beta}_1 = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})} {\sum_{i=1}^{n}(x_i-\bar{x})^2}, \qquad \hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x}. \]
For the dataset I chose the “diamonds” dataset used in previous lectures. Here I fit in a SLR of price of carat and the price increase.
data("diamonds")
smaller <- subset(diamonds, carat <= 3)
mod_d <- lm(price ~ carat, data = smaller)
summary(mod_d)
## ## Call: ## lm(formula = price ~ carat, data = smaller) ## ## Residuals: ## Min 1Q Median 3Q Max ## -14644.6 -813.9 -9.8 560.6 12711.3 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -2301.41 12.97 -177.4 <2e-16 *** ## carat 7819.33 14.03 557.3 <2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 1531 on 53906 degrees of freedom ## Multiple R-squared: 0.8521, Adjusted R-squared: 0.8521 ## F-statistic: 3.106e+05 on 1 and 53906 DF, p-value: < 2.2e-16
Here I showed the relationship between carat and price using a scatter plot with a linear regression line.
ggplot(smaller, aes(x = carat, y = price)) + geom_point(alpha = 0.25) + geom_smooth(method = "lm", se = FALSE, color = "darkred") + labs(title = "Diamonds: Price vs Carat", x = "Carat", y = "Price (USD)")
With residual standard error \(s\) and \(SS_x=\sum_{i=1}^{n}(x_i-\bar{x})^2\):
\[ SE(\hat{\beta}_1)=\sqrt{\frac{s^2}{SS_x}}, \qquad t=\frac{\hat{\beta}_1}{SE(\hat{\beta}_1)}, \qquad \text{CI: } \hat{\beta}_1 \pm t_{1-\alpha/2,\,n-2}\, SE(\hat{\beta}_1). \]
y <- smaller$price x <- smaller$carat n <- length(y) SSx <- sum( (x - mean(x))^2 ) b1 <- sum( (x - mean(x)) * (y - mean(y)) ) / SSx b0 <- mean(y) - b1*mean(x) e <- y - (b0 + b1*x) s2 <- sum(e^2)/(n-2); s <- sqrt(s2) SEb1 <- sqrt(s2/SSx) tcrit <- qt(p = 0.05, df = n-2, lower.tail = FALSE) # ~95% two-sided CI_b1 <- c(b1 - tcrit*SEb1, b1 + tcrit*SEb1) ### Print b0; b1; SEb1; CI_b1
## [1] -2301.412
## [1] 7819.326
## [1] 14.03087
## [1] 7796.247 7842.405
ggplot(smaller, aes(carat, price)) + geom_point(alpha = 0.25) + geom_smooth(se = TRUE) + labs(title = "Visual check: is the linear trend reasonable? Carat vs Price", x = "Carat", y = "Price (USD)")
data(trees) m <- lm(Volume ~ Girth, data = trees) plot_ly(trees, x = ~Girth, y = ~Volume, type = "scatter", mode = "markers", name = "data") %>% add_lines(x = ~Girth, y = ~fitted(m), name = "fitted") %>% layout(title = "Trees: Volume vs Girth (interactive)")
Assumptions:
Knowing gold carats and prices going up as the carat went up, I assumed the same for diamonds and so wanted to put it on the test.
Linearity: the mean of \(y\) changes linearly with \(x\).
Constant variance: residual spread is roughly constant across carat.
Takeaways:
The carat and price relationship is strong, positive, and approximately linear in the subset. The slope with the CI justified the assumptions
Price–carat relationship is strong and positive (R² ≈ 0.85).
Slope \(\hat\beta_1 \approx 7819\): ~$7.8k per +1 carat (subset).