We assume a linear relationship:
\[Sales_i = b_0 + b_1(Temp_i) + e_i\]
Overview
We assume a linear relationship:
\[Sales_i = b_0 + b_1(Temp_i) + e_i\]
Data
temp <- seq(60,100,length.out=30) sales <- 200 + 15*temp + rnorm(30,0,80) day <- 1:30 data <- data.frame(day,temp,sales) head(data)
## day temp sales ## 1 1 60.00000 1055.162 ## 2 2 61.37931 1102.275 ## 3 3 62.75862 1266.076 ## 4 4 64.13793 1167.710 ## 5 5 65.51724 1193.102 ## 6 6 66.89655 1340.653
Fit Model
model <- lm(sales ~ temp, data=data) summary(model)
## ## Call: ## lm(formula = sales ~ temp, data = data) ## ## Residuals: ## Min 1Q Median 3Q Max ## -148.64 -56.21 -9.89 45.07 147.71 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 310.386 96.361 3.221 0.00323 ** ## temp 13.573 1.191 11.393 5.02e-12 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 77.9 on 28 degrees of freedom ## Multiple R-squared: 0.8226, Adjusted R-squared: 0.8162 ## F-statistic: 129.8 on 1 and 28 DF, p-value: 5.015e-12
Plot 1
ggplot(data,aes(temp,sales)) +
geom_point(color="blue") +
geom_smooth(method="lm", color="red") +
labs(x="Temperature (F)", y="Sales ($)",
title="Ice Cream Sales vs Temperature")
Plot 2
plot(model$fitted.values, resid(model),
xlab="Fitted", ylab="Residuals",
main="Residuals vs Fitted")
abline(h=0,lty=2)
Plot 3
plot_ly(data, x=~temp, y=~day, z=~sales,
type="scatter3d", mode="markers",
marker=list(color="orange", size=4)) %>%
layout(title="Sales vs Temperature vs Day",
scene=list(
xaxis=list(title="Temperature"),
yaxis=list(title="Day"),
zaxis=list(title="Sales ($)")
))
Inference TEST: temperature predicts sales:
\[t = \frac{b_1 - 0}{SE(b_1)}\]
with this we can determine that temperature does affect sales.
Prediction
predict(model, data.frame(temp=85), interval="prediction")
## fit lwr upr ## 1 1464.097 1301.428 1626.766
Conclusion