2023-02-04

New tests v. New cases Code for Scatter plot and linear regression

mod = lm(new_cases ~ new_tests, data=covid)  # linear model
x <- covid$new_tests
y <- covid$new_cases
xax <- list( 
  title = "New Tests", 
  titlefont = list(family="Modern Computer Roman"), 
  range= c(0,3000000)) 
yax <- list( 
  title = "New Cases", 
  titlefont = list(family="Modern Computer Roman"),
 range= c(0,400000)) 
# Plot with linear regression 
fig <- plot_ly(x=x, y=y, type="scatter", mode="markers", name="data",  
        width=800, height=430) %>% 
        add_lines(x = x, y = fitted(mod), name="fitted") %>% 
        layout(xaxis = xax, yaxis = yax) %>% 
        layout(margin=list(l=100, r=50,b=20,t=10)) 

Latex Code for Linear Regression

The regression equation is

\[\\\hat{Y}_i = \hat{\beta}_0 + \hat{\beta}_1 X_i\]

And the least square estimates are

\[\\\hat{\beta}_1 = \frac{\sum(X_i – \bar{X}) (Y_i – \bar{Y})} {\sum(X_i – \bar{X})^2}\]

y-intercept equation is

\[\\\hat{\beta}_0 = \bar{Y} – \hat{\beta}_1 \bar{X}\]

New tests v. New cases Graph

config(fig, displaylogo=FALSE)

Latex Code for Linear Regression

The simplified linear equation is

\[\\{y}_i = m\beta_0 +\mathcal{E}\]

Matrix for linear regression is

\[ \begin{bmatrix} y_1 \\ y_2 \\ \dots \\ y_n \end{bmatrix} = \begin{bmatrix} 1 & x_{1} \\ 1 & x_{2} \\ \dots & \dots\\ 1 & x_{n} & \end{bmatrix} * \begin{bmatrix} \beta_0 \\ \beta_1 \end{bmatrix} + \begin{bmatrix} \mathcal{E}_{1} \\ \mathcal{E}_{2} \\ \dots \\ \mathcal{E}_{n} \end{bmatrix} \]

The final linear regression equation is

\[ \\y_i =\beta_0+\beta_1X_i+\mathcal{E} \]

New cases v. New Deaths Code Scatter Plot

# Scatter plot 1 using ggplot function 
x <- covid$new_cases
y <- covid$new_deaths
g = ggplot(covid, aes(x= new_cases, y=new_deaths))+  geom_point(aes(color=icu_patients))+ 
  ggtitle('New Cases v. New Deaths')

New cases v. New Deaths Graph

New tests v. Positive Rate Code

# Scatter plot 2 using ggplot function 
x <- covid$new_tests
y <- covid$positive_rate
g2 = ggplot(covid, aes(x=new_tests , y=positive_rate)) + geom_point(aes(color=new_deaths)) + 
  ggtitle('New Tests v. Positive Rate')

New tests v. Positive Rate Code Scatter Plot

## Warning: Removed 6 rows containing missing values (`geom_point()`).

New cases v. New Deaths v. New tests Code

# 3D scatter plot using plotly function 
my.plot <- plot_ly(covid,
                   type = "scatter3d",
                   mode = "markers",
                   x = ~new_cases,
                   y = ~new_deaths,
                   z = ~new_tests,
                   color = ~icu_patients)
my.plot <- my.plot %>% layout(title = 'New cases v. New Deaths v. New tests',autosize = TRUE,
                              scene = list(xaxis = list(title = "Cases"),
                                           yaxis = list(title = "Deaths"), 
                                           zaxis = list(title = "Tests")))

New cases v. New Deaths v. New tests v. ice patients 3D model