---
title: 'Appendices: New York Restaurants Analyses'
author: "Brian Junker"
date: "9/1/2020"
output:
  pdf_document:
    toc: yes
  html_document:
    df_print: paged
  word_document:
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Appendix 1. Initial Data Import & Exploration

Read the data in, get a general sense of the variables, and make a "pairs" plot (scatterplot matrix) of the numerical variables.  Note that "Price" is the response variable. 
```{r read-and-look}
nyc <- read.csv("nyc.csv")

str(nyc)

summary(nyc)

pairs(nyc[,-c(1:2,7)])

```

We can get a more refined look at the variables with a scatterplot matrix that also includes histograms for each variable.  If the histograms revealed especially long tails or wierd outliers, we might want to transform the data, recode or delete outliers, etc.

```{r better-pairs-plot}
library(psych)  
## you would need to install the "psych" package 
## one time before using this library() command...

pairs.panels(nyc[,-c(1:2,7)],
             method = "pearson",    ## correlation method
             hist.col = "#00AFBB",  ## a pretty color for histogram bars...
             density = TRUE,        ## show density plots
             ellipses = TRUE        ## show correlation ellipses
             )


```

(There are lots of other packages, including ggplot, that can produce similar plots. This is really just a convenient illustration.)

The histograms don't suggest any special processing (transformations, etc.) will be needed for the variables, so we can proceed.  Note that all the variables seem fairly highly correlated with one another, which makes sense, but also can affect regression results, as we'll learn later in the semester.

One of the main questions for this study is whether restaurants shoudl locate east or west of Fifth Avenue.  A pair of boxplots give us a first look at this question:
```{r boxplots}
with(nyc,boxplot(Price ~ East, xlab="East (1 = East of Fifth Avenue)"))

```

## Appendix 2. Analysis of Apparent Outliers in the EDA Plots

To find the two restaurants with modest Service ratings and maximal dinner Prices...
```{r high-price}
nyc[nyc$Price==max(nyc$Price),]
```

To find the restaurant with Service = 15....
```{r mediocre-service}
nyc[nyc$Service==15,]
```




## Appendix 3. Regression Analysis -- Main Effects Only

Here's a very light regression analysis to see how the variables work with one another.

```{r regression-main-effects}
summary(lm.0 <- lm(Price ~ . , data=nyc[,-c(1,2)]))

par(mfrow=c(2,2))

plot(lm.0)

```

This model, which just has main effects for each of the quantitative predictor variables, suggests some interesting effects on menu prices, and the residual (casewise) diagnostic plots don't show any dramatic misfit, outliers, influential observations, etc.

From the table of coefficients, it looks like Food and Decor matter a lot for Price, but Service does not.  This may be because Service is highly correlated with Food (and with Decor for that matter...).

There is also an effect for being East of Fifth Avenue; this is different from the result we got using only boxplots, because boxplots compare the whole distribution (and so differences have to be true across the distribution of prices) whereas regression analysis basically just looks at means, adjusted for the other variables in the model.  Generally when you concentrate inference on the means, you get more dramatic results (because, roughly speaking, $SE_{mean} = SD_{population}/\sqrt{sample~size}$).

If you are a policy maker (say, you have a lot of money and you are going to open several restaurants), you may care more about the fact that the mean price can be higher East of Fifth Avenue, since on average you can charge a bit more in your restaurants.

On the other hand if you are considering opening just one restaurant, the story of the boxplots may be more important: the price distributions for East vs West restaurants greatly overlap, there's little reason to make a location East of Fifth Avenue a primary concern.

## Appendix 4. Regression analysis -- Two-Way Interactions

Just for fun, we'll also try the model that has all main effects and two-way interactions, and we'll compare the two models with  likelihood ratio test.
```{r regression-interactions}
summary(lm.1 <- lm(Price ~ .^2 , data=nyc[,-c(1,2)]))

par(mfrow=c(2,2))

plot(lm.1)
```

This model is intersting in that several interactions seem to have coefficients significantly different from zero, and some of the main effects no longer do.  The residual plots do not look much better (or worse) than the plots for the main-effects-only model.

As a rule, unless you have a VERY VERY VERY VERY good reason for doing otherwise, when you want to keep an interaction in a model you should also keep the main effects.  Thus, if we wanted to keep the Service:East interaction, we should also keep the main variables Service and East, even though neither main effect is significantly different from zero.

However, in this analysis we do not need to worry so much about that, since the likelihood ratio test does not strongly favor the model with interactions; it appears we can "get away" with just the main effects models.
```{r LR-test}
anova(lm.0,lm.1,test="LRT")
```

So, we can just stick with the simpler model, lm.0.


## Appendix 5. Predicting the price of a restaurant that has very high scores on food, Decor and Service...

```{r prediction}
low.premium <- data.frame(Case=1000,Restaurant="The Ritz!",Price=NA,
                          Food=25,Decor=25,Service=25,East=0)
predict(lm.0,low.premium,interval="prediction")

high.premium <- data.frame(Case=1000,Restaurant="The Ritz!",Price=NA,
                           Food=30,Decor=30,Service=30,East=1)
predict(lm.0,high.premium,interval="prediction")

```


## Appendix 6. A Table Suitable for Including in a Report

```{r just-the-coefficients}

round(summary(lm.0)$coefficients,3)

```

