Assignment 10 - plotly & Uncertainty

Group 10, Michael Vaden, mtv2eva

Due Date: 11:59pm, Mar 20

Group Homework

Group Homework

Part 1

Part 1: Instruction

Part 1: Example

Part 1: Results

library(tidyr) # load tidyr package
library(plotly) # load plotly package

data(EuStockMarkets) # load EuStockMarkets
dat <- as.data.frame(EuStockMarkets) # coerce it to a data frame
dat$time <- time(EuStockMarkets) # add `time` variable
#View(dat)

# add your codes
market_dat <- dat %>% gather("market", "price", 1:4)

dat_plot <- plot_ly(market_dat, x = ~time, y = ~price, color= ~market, type = 'scatter', mode = 'lines')
dat_plot <- dat_plot %>% layout(title = 'Stock Closing Prices Over Time',
         xaxis = list(title = 'Time'),
         yaxis = list (title = 'Price'))
dat_plot

Part 2

Part 2: Instruction

# add your codes
library(foreign)
library(boot)

data <- read.spss("/Users/michaelvaden/Downloads/SCS_QE.sav", to.data.frame=TRUE)
## re-encoding from CP1252
b.stat <- function(data, i)
{
   b.dat <- data[i ,]
   out.lm <- lm(mathpre ~ mars, b.dat)
   predict(out.lm, data.frame(mars=data$mars))   
}

b.out <- boot(data, b.stat, R=2000)
boot.ci(b.out, index = 1, type = 'perc')
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 2000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = b.out, type = "perc", index = 1)
## 
## Intervals : 
## Level     Percentile     
## 95%   ( 6.147,  6.929 )  
## Calculations and Intervals on Original Scale
b.ci <- t(sapply(1:nrow(data), function(x) boot.ci(b.out, index = x, type = 'perc')$percent))[, 4:5]
dimnames(b.ci) <- list(rownames(data), c('lower', 'upper'))

scs2 <- cbind(data, b.ci)

ggplot(scs2, aes(x=mars, y=mathpre)) + geom_jitter(alpha=0.2) + 
  labs(x = 'Math Achievement Score', y = 'Math Anxiety Score') + 
        geom_smooth(method='lm', formula= y~x, se = FALSE) + theme_bw() +
        geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.3, fill="#69b3a2")

Part 3

Part 3: Instruction

Part 3: Instruction (Cont’d)

# add your codes
#file.choose()
who <- read.csv("/Users/michaelvaden/Downloads/WHO-COVID-19-global-data.csv")
#View(who)

who <- who[who$Date_reported == max(who$Date_reported),]

who$Country[who$Country =="United States of America"] = "US"
who$Country[who$Country =="Iran (Islamic Republic of)"] = "Iran"
who$Country[who$Country =="The United Kingdom"] = "UK"

regions <- who[ who$Country == "Ethiopia" | # Africa
                           who$Country == "Congo" |  
                           who$Country == "Nigeria" | 
                           who$Country == "US" |     # Americas
                           who$Country == "Mexico" | 
                           who$Country == "Brazil" |
                           who$Country == "Pakistan" | # Eastern Mediterranean
                           who$Country == "Iran" | 
                           who$Country ==  "Egypt" |
                           who$Country ==  "Germany" | # Europe
                           who$Country == "UK" | 
                           who$Country == "France" |
                           who$Country == "India" |   # South-East Asia
                           who$Country == "Thailand" |  
                           who$Country == "Indonesia" |  
                           who$Country == "China" |   # Western Pacific
                           who$Country == "Japan" | 
                           who$Country == "Australia",]

regions <- regions %>% mutate(rate = Cumulative_deaths/Cumulative_cases, 
                                    SE = sqrt(rate*(1-rate)/Cumulative_cases))

regions[is.na(regions)] = 0 # remove missing values

ggplot(regions, aes(x=rate, y=Country, fill=WHO_region)) + geom_col() + theme_bw() + 
  xlab("Reported Deaths / Reported Cases") + 
  facet_grid(vars(WHO_region), scale="free") +
  geom_errorbar(aes(xmin=rate-1.96*SE, xmax=rate+1.96*SE), width=.2)

Part 3: Example