set.seed(123)
temperature <- seq(15, 35, length.out = 100)
sales <- 25 + 1.5 * temperature + rnorm(100, mean = 0, sd = 5)
data <- data.frame(temperature, sales)
library(ggplot2)
ggplot(data, aes(x = temperature, y = sales)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
theme_minimal() +
ggtitle("Ice Cream Sales vs. Temperature")
## `geom_smooth()` using formula = 'y ~ x'

fit <- lm(sales ~ temperature, data = data)
summary(fit)
##
## Call:
## lm(formula = sales ~ temperature, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.2678 -2.7618 -0.1731 3.2425 10.4744
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.89830 2.01295 11.87 <2e-16 ***
## temperature 1.56215 0.07841 19.92 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.573 on 98 degrees of freedom
## Multiple R-squared: 0.802, Adjusted R-squared: 0.8
## F-statistic: 396.9 on 1 and 98 DF, p-value: < 2.2e-16