```{r}
#| echo: false
#| message: false
library(tidyverse)
library(broom)
library(openintro)
```

# Case Study: Mario Kart

We’ll consider Ebay auctions of a video game called Mario Kart for the Nintendo Wii. The outcome variable of interest is the total price of an auction, which is the highest bid plus the shipping cost. We will try to determine how total price is related to each characteristic in an auction while simultaneously controlling for other variables.

## Dataset and Visualization

```{r}
df <- mariokart %>% 
  select(c(total_pr, cond, stock_photo, duration, wheels)) %>%
  mutate(cond = (as.integer(cond)-2)*(-1), stock_photo = as.integer(stock_photo)-1) %>% 
  filter(total_pr<100)
glimpse(df)
```
## Variables Description
- `total_pr`: Final auction price plus shipping costs, in US dollars.
- `cond`: Indicator variable for if the game is new (1) or used (0).
- `stock_photo`: Indicator variable for if the auction’s main photo is a stock photo.
- `duration`: The length of the auction, in days, taking values from 1 to 10.
- `wheels`: The number of Wii wheels included with the auction. A Wii wheel is an optional steering wheel accessory that holds the Wii controller.

## Simple Linear Regression for numeric explanatory variable

### Variables

- Response: total_pr (numeric)
- Explanatory: wheels (numeric)

```{r}
ggplot(df,
       aes(x = wheels, y = total_pr)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE)
```

### Model

- Model:

$total\_pr=\beta_0+\beta_1wheels+\epsilon$

- Fitted Model:

$\hat {total\_pr}=b_0+b_1wheels$

```{r}
slr <- lm(total_pr ~ wheels, data = df)
broom::tidy(slr)
```

### Interpretation of estimates

- $b_1=8.64$: For one more wheel, the expected increase in the price is 8.64 dollar.
- $b_0=37.50$: For a Mario Kart with zero wheel, the expected price is 37.50 dollar.

### Inference

#### Hypothesis Testing

$H_0:\beta_1=0$ vs. $H_0:\beta_1 \neq 0$

$p-value \approx 0<0.05$, so we reject the null hypothesis and conclude that there is a significant relationship between wheel and price.

#### Confidence Interval

95% CI for $\beta_1$ = [point estimate $\pm$ critical value $\times$ SE]

- point estimate: $b_1=8.642731$
- critical value: $t^*=qt(0.975, df = n-1=141-1)$
- SE: $0.5479454$

```{r}
8.642731 - qt(0.975, df = 140) * 0.5479454
8.642731 + qt(0.975, df = 140) * 0.5479454
```

or equivalently in R code:

```{r}
confint(slr)
```

## Simple Linear Regression for categorical explanatory variable

### Variables

- Response: total_pr (numeric)
- Explanatory: cond (categorical)

### Model

- Model:

$total\_pr=\beta_0+\beta_1cond+\epsilon$

- Fitted Model:

$\hat {total\_pr}=b_0+b_1cond$

```{r}
slr <- lm(total_pr ~ cond, data = df)
broom::tidy(slr)
```

### Interpretation of estimates

- $b_1+b_0=10.90+42.87=53.77$: Expected price for a new game.
- $b_0=42.87$: Expected price for an old game.
- $b_1=10.90$: Expected price difference between a new and an old game.

### Inference

#### Hypothesis Testing

$H_0:\beta_1=0$ vs. $H_0:\beta_1 \neq 0$

$p-value \approx 0<0.05$, so we reject the null hypothesis and conclude that there is a significant relationship between condition and price.

#### Confidence Interval

```{r}
confint(slr)
```

## Multiple Linear Regression

```{r}
mlr <- lm(total_pr ~ wheels + cond + stock_photo + duration, data = df)
broom::tidy(mlr)
```

### Model Evaluation

A simpler model:

```{r}
mlr_2 <- lm(total_pr ~ wheels + cond, data = df)
broom::tidy(mlr_2)
```

#### R squared and Adjusted R squared

- R squared: 72% of the variation in price is explained by the model. Higher values of R squared is preferred, however, R squared always goes up when keeping adding explanatory variables into the model.

- Adjusted R squared: When comparing models with multiple predictors we rely on Adjusted R-squared.


```{r}
glance(mlr)$r.squared
glance(mlr_2)$r.squared
glance(mlr)$adj.r.squared
glance(mlr_2)$adj.r.squared
```





