---
title: "Session 4 R practice"
author: "Dr. Richard M. Crowley"
date: "ACCT 420, Fall 2019, Session 4"
output:
  html_document:
    theme: spacelab
    highlight: default
    toc: true
    toc_float:
      collapsed: false
    self_contained: false
---

```{r, include=FALSE}
tutorial::go_interactive()
```

## Logistic models

To run your code, click *run*.  It will let you know if your code is correct or not, and will offer hints on what to fix if needed.  If you are stuck, you can see the solution by clicking *Solution*.

### Exercise 1: Using mutate()

Reminder:

$$
yoy\_growth = \frac{level_t - level_{t-4}}{level_{t-4}}
$$

```{r ex="create_a", type="pre-exercise-code"}
df <- data.frame(tic=rep("WMT",194),
                 fyearq=c(1970,1970,1970,1970,1971,1971,1971,1971,1972,1972,1972,1972,1973,1973,1973,1973,1974,1974,1974,1974,1975,1975,1975,1975,1976,1976,1976,1976,1977,1977,1977,1977,1978,1978,1978,1978,1979,1979,1979,1979,1980,1980,1980,1980,1981,1981,1981,1981,1982,1982,1982,1982,1983,1983,1983,1983,1984,1984,1984,1984,1985,1985,1985,1985,1986,1986,1986,1986,1987,1987,1987,1987,1988,1988,1988,1988,1989,1989,1989,1989,1990,1990,1990,1990,1991,1991,1991,1991,1992,1992,1992,1992,1993,1993,1993,1993,1994,1994,1994,1994,1995,1995,1995,1995,1996,1996,1996,1996,1997,1997,1997,1997,1998,1998,1998,1998,1999,1999,1999,1999,2000,2000,2000,2000,2001,2001,2001,2001,2002,2002,2002,2002,2003,2003,2003,2003,2004,2004,2004,2004,2005,2005,2005,2005,2006,2006,2006,2006,2007,2007,2007,2007,2008,2008,2008,2008,2009,2009,2009,2009,2010,2010,2010,2010,2011,2011,2011,2011,2012,2012,2012,2012,2013,2013,2013,2013,2014,2014,2014,2014,2015,2015,2015,2015,2016,2016,2016,2016,2017,2017,2017,2017,2018,2018),
                 fqtr=c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2),
                 revtq=c(7.267,9.939,11.47,15.61,12.758,18.207,19.205,27.844,21.011,29.918,32.683,41.277,32.737,37.655,42.098,55.071,46.355,56.177,59.462,74.215,64.65,78.852,86.108,110.721,93.971,115.186,121.875,147.775,122.655,152.381,172.637,230.783,167.274,204.085,221.982,306.957,238.569,291.685,300.747,417.175,314.575,372.598,401.842,554.184,434.251,533.653,634.476,842.617,665.229,788.705,827.767,1094.551,854.734,1098.942,1166.594,1546.639,1234.768,1508.533,1583.573,2073.983,1655.661,1934.358,2087.533,2773.936,2344.253,2770.276,2949.008,3845.557,3211.761,3732.237,4015.017,5000.238,4282.012,4845.133,4991.113,6530.734,5373.258,6046.41,6283.496,8107.484,6768.191,7543.508,7930.949,10358.898,9280.566,10339.898,10627.5,13638.797,11649.398,13028.398,13683.797,17122,13920.398,16236.5,16826.898,20360.5,17686.098,19942.297,20417.699,24447.797,20440,22723,22914,27550,22772,25587,25644,30856,25409,28386,28777,35386,29819,33521,33509,40785,35129,38913,40899,51868,43447,46588,46181,57079,48565,53187,53185,64735,52126,56781,55765,66905,57224,63231,63036,75190,65434,70516,69282,82900,70755,76697,75397,89252,79675,85430,84467,99078,86378,92827,91794,107289,94940,102342,98345,108747,94214,100876,99373,113622,99811,103726,101952,116360,104189,109366,110226,123169,113010,114065,113800,127776,114070,116830,115688,129706,114960,120125,119001,131565,114826,120229,117408,129667,115904,120319,118179,130936,117542,122968,123179,136267,122690,128028),
                 recession=c(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
```

```{r ex="create_a", type="sample-code"}
# The dataframe below contains historical revenue for Walmart since 1970
# All data is quarterly
# Recession data comes from the US Federal Reserve
# The file uses Compustat's nomenclature:
#     - revenue is revtq
#     - ticker is tic
#     - fyearq is year
#     - fqtr is quarter
#     - recession is an indicator for US recessions
df[1,5]

library(dplyr) # loads dplyr for data manipulation

# Create a variable for year over year revenue growth by quarter
# Store the result in a data frame column named revtq_yoy


# Create an indicator equal to 1 if revtq_yoy is less than 0, and 0 otherwise
# Using ifelse makes this easy to do
# Store the result in a data frame column named revtq_down


# Create a lag of recession, call it recession_lag


#View the output
head(df)

#END
```

```{r ex="create_a", type="solution"}
# The dataframe below contains historical revenue for Walmart since 1970
# All data is quarterly
# Recession data comes from the US Federal Reserve
# The file uses Compustat's nomenclature:
#     - revenue is revtq
#     - ticker is tic
#     - fyearq is year
#     - fqtr is quarter
#     - recession is an indicator for US recessions
df[1,5]

library(dplyr) # loads dplyr for data manipulation

# Create a variable for year over year revenue growth by quarter
# Store the result in a data frame column named revtq_yoy
df <- df %>% mutate(revtq_yoy = (revtq - lag(revtq, 4)) / lag(revtq, 4))

# Create an indicator equal to 1 if revtq_yoy is less than 0, and 0 otherwise
# Store the result in a data frame column named revtq_down
df <- df %>% mutate(revtq_down = ifelse(revtq_yoy<0,1,0))

# Create a 1 quarter lag of recession, call it recession_lag
df <- df %>% mutate(recession_lag = lag(recession))

#View the output
head(df)

#END
```

```{r ex="create_a", type="sct"}
# Template based on https://www.rdocumentation.org/packages/testwhat/versions/4.1.1
# Check if something is explicitly typed

test_student_typed("<-", not_typed_msg='Did you remember to store the results back into the data frame?')
test_expression_output("'revtq_yoy' %in% colnames(df)", incorrect_msg="Did you remember to save the output to the dataframe, and to name the column `revtq_yoy`?")
test_expression_output("df$revtq_yoy", incorrect_msg="It looks like either the column name is wrong or the calculation is wrong for `revtq_yoy`.")

test_expression_output("'revtq_down' %in% colnames(df)", incorrect_msg="Did you remember to save the output to the dataframe, and to name the column `revtq_down`?")
test_expression_output("df$revtq_down", incorrect_msg="It looks like either the column name is wrong or the calculation is wrong for `revtq_down`.")

test_expression_output("'recession_lag' %in% colnames(df)", incorrect_msg="Did you remember to save the output to the dataframe, and to name the column `recession_lag`?")
test_expression_output("df$recession_lag", incorrect_msg="It looks like either the column name is wrong or the calculation is wrong for `recession_lag`.")
test_student_typed('head(df)', not_typed_msg='Don\'t remove the code `head(df)`.')

# test_student_typed('x <- 2', not_typed_msg='')

# Check if function was used in input code
# test_function('c',incorrect_msg='')  

# Requires an object `x` to have the same value as the solution
# test_object("x",incorrect_msg = "",undefined_msg = "")  

# Requires an onject with the same value of `x` in the solution
# test_an_object("x",undefined_msg="")

# Checks if output of student's code contains given evaluated expression
# test_output_contains("x",incorrect_msg = "")

# Check if a vector of predefined objects are unchanged
# test_predefined_objects(c('x','y'),incorrect_msg="Don't onverwrite the predefined variables")

# Checks for a regex pattern in trhe output
# test_output_regex(pattern,fixed=F, times=1, incorrect_msg='')

# Can check an arbitrary expression across both solution and student code
#test_expression_output("typeof(company_name)", incorrect_msg="Did you store textual data in `company_name`?")

test_error()
success_msg("Awesome!")

# Other functions to note:
#     - test_or(a,b) -- checks if either test a or test b pass
#     - test_ggplot() -- can check if plots are correct
#     - test_function() -- can also check included parameters
#     - test_loop() -- checking for and while loops
#     - test_library_function('package', not_called_msg='',incorrect_msg='')
#     - test_if_else() -- checking if statements
#     - test_expression_error() -- can check if functions are properly defined
#     - test_operator('operator',), not_called_msg='',incorrect_msg='')
#     - test_function_definition() -- rigorously check defined function
#     - test_data_frame() -- check if dataframe [columns] are equivalent
#     - test_function_result, test_expression_result
```


### Exercise 2: Running a logit regression

```{r ex="create_b", type="pre-exercise-code"}
library(dplyr) # loads dplyr for data manipulation
df <- data.frame(tic=rep("WMT",194),
                 fyearq=c(1970,1970,1970,1970,1971,1971,1971,1971,1972,1972,1972,1972,1973,1973,1973,1973,1974,1974,1974,1974,1975,1975,1975,1975,1976,1976,1976,1976,1977,1977,1977,1977,1978,1978,1978,1978,1979,1979,1979,1979,1980,1980,1980,1980,1981,1981,1981,1981,1982,1982,1982,1982,1983,1983,1983,1983,1984,1984,1984,1984,1985,1985,1985,1985,1986,1986,1986,1986,1987,1987,1987,1987,1988,1988,1988,1988,1989,1989,1989,1989,1990,1990,1990,1990,1991,1991,1991,1991,1992,1992,1992,1992,1993,1993,1993,1993,1994,1994,1994,1994,1995,1995,1995,1995,1996,1996,1996,1996,1997,1997,1997,1997,1998,1998,1998,1998,1999,1999,1999,1999,2000,2000,2000,2000,2001,2001,2001,2001,2002,2002,2002,2002,2003,2003,2003,2003,2004,2004,2004,2004,2005,2005,2005,2005,2006,2006,2006,2006,2007,2007,2007,2007,2008,2008,2008,2008,2009,2009,2009,2009,2010,2010,2010,2010,2011,2011,2011,2011,2012,2012,2012,2012,2013,2013,2013,2013,2014,2014,2014,2014,2015,2015,2015,2015,2016,2016,2016,2016,2017,2017,2017,2017,2018,2018),
                 fqtr=c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2),
                 revtq=c(7.267,9.939,11.47,15.61,12.758,18.207,19.205,27.844,21.011,29.918,32.683,41.277,32.737,37.655,42.098,55.071,46.355,56.177,59.462,74.215,64.65,78.852,86.108,110.721,93.971,115.186,121.875,147.775,122.655,152.381,172.637,230.783,167.274,204.085,221.982,306.957,238.569,291.685,300.747,417.175,314.575,372.598,401.842,554.184,434.251,533.653,634.476,842.617,665.229,788.705,827.767,1094.551,854.734,1098.942,1166.594,1546.639,1234.768,1508.533,1583.573,2073.983,1655.661,1934.358,2087.533,2773.936,2344.253,2770.276,2949.008,3845.557,3211.761,3732.237,4015.017,5000.238,4282.012,4845.133,4991.113,6530.734,5373.258,6046.41,6283.496,8107.484,6768.191,7543.508,7930.949,10358.898,9280.566,10339.898,10627.5,13638.797,11649.398,13028.398,13683.797,17122,13920.398,16236.5,16826.898,20360.5,17686.098,19942.297,20417.699,24447.797,20440,22723,22914,27550,22772,25587,25644,30856,25409,28386,28777,35386,29819,33521,33509,40785,35129,38913,40899,51868,43447,46588,46181,57079,48565,53187,53185,64735,52126,56781,55765,66905,57224,63231,63036,75190,65434,70516,69282,82900,70755,76697,75397,89252,79675,85430,84467,99078,86378,92827,91794,107289,94940,102342,98345,108747,94214,100876,99373,113622,99811,103726,101952,116360,104189,109366,110226,123169,113010,114065,113800,127776,114070,116830,115688,129706,114960,120125,119001,131565,114826,120229,117408,129667,115904,120319,118179,130936,117542,122968,123179,136267,122690,128028),
                 recession=c(1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
df <- df %>% mutate(revtq_yoy = (revtq - lag(revtq, 4)) / lag(revtq, 4))
df <- df %>% mutate(recession_lag = lag(recession))
df <- df %>% mutate(revtq_down = ifelse(revtq_yoy<0,1,0))
```

```{r ex="create_b", type="sample-code"}
# The dataframe below contains historical revenue for Walmart since 1970
# All data is quarterly
# Recession data comes from the US Federal Reserve
# The file uses Compustat's nomenclature:
#     - revenue is revtq
#     - ticker is tic
#     - fyearq is year
#     - fqtr is quarter
#     - recession is an indicator for US recessions
# It also contains revtq_yoy, revtq_down, and recession_lag from the first exercise
df[1:5,]

# Calculate a logistic model of a decrease in year over year quarterly revenue
# on the presence of a recession last quarter, store it in mod1
mod1 <- glm( ~ , data=, family=)

# Print out a summary of the model using summary


#END
```

```{r ex="create_b", type="solution"}
# The dataframe below contains historical revenue for Walmart since 1970
# All data is quarterly
# Recession data comes from the US Federal Reserve
# The file uses Compustat's nomenclature:
#     - revenue is revtq
#     - ticker is tic
#     - fyearq is year
#     - fqtr is quarter
#     - recession is an indicator for US recessions
# It also contains revtq_yoy, revtq_down, and recession_lag from the first exercise
df[1:5,]

# Calculate a logistic model of a decrease in year over year quarterly revenue
# on the presence of a recession last quarter, store it in mod1
mod1 <- glm(revtq_down ~ recession_lag, data=df, family=binomial)

# Print out a summary of the model using summary
summary(mod1)

#END
```

```{r ex="create_b", type="sct"}
# Template based on https://www.rdocumentation.org/packages/testwhat/versions/4.1.1
# Check if something is explicitly typed

test_student_typed('revtq_down ~ recession_lag',not_typed_msg='Make sure to regress `revtq_down` on `recession_lag`.')
test_student_typed('data=df',not_typed_msg='Make sure to using the `df` data frame for the test.')
test_student_typed('family=binomial',not_typed_msg='Make sure to set family to be `binomial`, so as to run a logistic regression.')
test_expression_output("mod1", incorrect_msg="Your regression model isn't quite right.")
test_student_typed('summary(mod1)',not_typed_msg='Your model is correct, but did you remember to summarize it with `summary()`?')

# test_student_typed('x <- 2', not_typed_msg='')

# Check if function was used in input code
# test_function('c',incorrect_msg='')  

# Requires an object `x` to have the same value as the solution
# test_object("x",incorrect_msg = "",undefined_msg = "")  

# Requires an onject with the same value of `x` in the solution
# test_an_object("x",undefined_msg="")

# Checks if output of student's code contains given evaluated expression
# test_output_contains("x",incorrect_msg = "")

# Check if a vector of predefined objects are unchanged
# test_predefined_objects(c('x','y'),incorrect_msg="Don't onverwrite the predefined variables")

# Checks for a regex pattern in trhe output
# test_output_regex(pattern,fixed=F, times=1, incorrect_msg='')

# Can check an arbitrary expression across both solution and student code
#test_expression_output("typeof(company_name)", incorrect_msg="Did you store textual data in `company_name`?")

test_error()
success_msg("Awesome!")

# Other functions to note:
#     - test_or(a,b) -- checks if either test a or test b pass
#     - test_ggplot() -- can check if plots are correct
#     - test_function() -- can also check included parameters
#     - test_loop() -- checking for and while loops
#     - test_library_function('package', not_called_msg='',incorrect_msg='')
#     - test_if_else() -- checking if statements
#     - test_expression_error() -- can check if functions are properly defined
#     - test_operator('operator',), not_called_msg='',incorrect_msg='')
#     - test_function_definition() -- rigorously check defined function
#     - test_data_frame() -- check if dataframe [columns] are equivalent
#     - test_function_result, test_expression_result
```

## Visualization with maps

### Exercise 3: Using Plotly for maps

**NOTE:** Unforetunately, though DataCamp light has `plotly` installed, it doesn't work at present (plots don't open).  As such, we can't run this here and see any output.  Instead, you can <a href="./mg.csv">download this file</a>, load the file into R with `read.csv()`, and then run the code given below.  Alternatively, you can download <a href="./s4ex3.Rmd">this Rmd file</a> containing this exercise's code.  The output of the code is <a href="./s4ex3.html">available here</a>.

You will need `plotly` installed to run the code.  If you do not have it installed, run `install.packages("plotly")`.

```{r ex="create_c", type="pre-exercise-code"}

```

```{r ex="create_c", type="sample-code"}
# The file mg.csv contains the following information for Typhoon Mangkhut:
#     - lat: latitude
#     - lon: longitude (fixed)
#     - dt: Date time in format "%Y%b%d %H%M%S"
mg <- read.csv("mg.csv")

# Sample of the data:
mg[1:5,]

# Load plotly for plotting
library(plotly)

# Settings for the plot -- complete, but some other options are listed in the comments
geo <- list(
  showland = TRUE,
  showlakes = TRUE,
  showcountries = TRUE,
  showocean = TRUE,
  countrywidth = 0.5,
  landcolor = toRGB("grey90"),  # Run colors() after loading plotly to see available colors
  lakecolor = toRGB("aliceblue"),
  oceancolor = toRGB("aliceblue"),
  projection = list(
    type = 'orthographic',  # Consider trying 'equirectangular' or 'steriographic'
    rotation = list(
      lon = 100,  # This sets the center longitude
      lat = 1,    # This sets the center latitude
      roll = 0
    )
  ),
  lonaxis = list(
    showgrid = TRUE,
    gridcolor = toRGB("gray40"),
    gridwidth = 0.5
  ),
  lataxis = list(
    showgrid = TRUE,
    gridcolor = toRGB("gray40"),
    gridwidth = 0.5
  )
)


# Plot
plot_geo() %>%
  add_markers(data=mg,
              x = ~lon,
              y = ~lat,
              text=~paste("Time: ", dt)) %>%
  layout(showlegend = TRUE, geo = geo,
    title = 'Typhoon Mangkhut (Sept 2018), full path')
```


## Exercise 4: Plotting with leaflet...

**NOTE:** Unforetunately, DataCamp light doesn't have `leaflet` installed.  As such, we can't run this here.  If you would like to see a nice graph of the typhoon path in leaflet, you can <a href="./mg.csv">download this file</a> (same csv as for Exercise 3) and <a href="./s4ex4.Rmd">this Rmd file</a> containing this exercise's code.  The output of the code is <a href="./s4ex4.html">available here</a>.

You will need `leaflet` installed to run the code.  If you do not have it installed, run `install.packages("leaflet")`

The final map also requires `leaflet.extras` installed.  If you do not have it installed, run `install.packages("leaflet.extras")`


```{r ex="create_e", type="pre-exercise-code"}

```

```{r ex="create_e", type="sample-code"}
# The file mg.csv contains the following information for Typhoon Mangkhut:
#     - lat: latitude
#     - lon: longitude (fixed)
#     - dt: Date time in format "%Y%b%d %H%M%S"
mg <- read.csv("mg.csv")

# Sample of the data:
mg[1:5,]

# Load leaflet for plotting
library(leaflet)

# Plot out the data -- basic graph
leaflet() %>%
  addTiles() %>% 
  setView(lng = 136, lat = 20, zoom=3) %>%
  addMarkers(data=mg, lng=~lon, lat=~lat, label = ~dt)

#END
```