Supplement
.Rmd
keyboard_arrow_up
School
Wake Tech *
*We aren’t endorsed by this school
Course
320
Subject
Industrial Engineering
Date
Dec 6, 2023
Type
Rmd
Pages
3
Uploaded by MateScience9335
---
title: "Supplement for Modeling 2"
author: "Mario Giacomazzo"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,warning=F)
options(scipen=999)
library(tidyverse)
#Essential Functions
library(modelr)
#Helpful Functions in Modeling
DATA=read_csv("AirWaterTemp.csv",col_types=cols()) #River Data
```
# Introduction
Today, we will work with daily water temperature and air temperature data observed
for `r length(unique(DATA$L))` rivers in Spain. The goal of this tutorial is to
identify the best model for predicting the maximum water temperature given the
maximum air temperature. In the preview below, `W` represents the daily maximum
water temperature and `A` represents the daily maximum air temperature. The data
contains almost a full year of data for each of the `r length(unique(DATA$L))`
different rivers.
```{r,echo=F}
head(DATA)
```
#Part 1: Examining the Relationship
##Chunk 1: Overall Relationship
```{r,echo=F,eval=T,message=F}
ggplot(data=DATA) +
geom_point(aes(x=A,y=W),alpha=0.3)+
geom_smooth(aes(x=A,y=W)) +
theme_minimal()
```
##Chunk 2: Location-Specific Relationship
```{r,echo=F,eval=F}
Location = 103
DATA %>% filter(L==Location) %>%
ggplot()+
geom_point(aes(x=A,y=W),alpha=0.3)+
geom_smooth(aes(x=A,y=W)) +
theme_minimal()
```
##Chunk 3: Split Data into Train and Test Sets
```{r,eval=F}
set.seed(INTEGER)
TEST.LOCATIONS=sample(x=unique(DATA$L),size=3,replace=F)
TRAIN = anti_join(DATA,tibble(L=TEST.LOCATIONS),by="L")
TEST = semi_join(DATA,tibble(L=TEST.LOCATIONS),by="L")
```
##Chunk 4: Plots of Relationship for Train and Test Data
```{r,echo=F,eval=F}
WAPLOT2.func=function(DATA){
ggplot(data=DATA)+
geom_point(aes(x=A,y=W),alpha=0.3)+
geom_smooth(aes(x=A,y=W)) +
theme_minimal()
}
WAPLOT2.func(TRAIN)
WAPLOT2.func(TEST)
```
#Part 2: Linear Regression Model
##Chunk 1: Fitting Linear Model to Train Data
```{r,echo=F,eval=F}
linmod=lm(W~A,data=TRAIN)
summary(linmod)
```
##Chunk 2: Getting Predictions from Linear Model
```{r,eval=F}
TRAIN2 = TRAIN %>% add_predictions(linmod,var="linpred")
TEST2 = TEST %>% add_predictions(linmod,var="linpred")
```
##Chunk 3: Getting Residuals from Linear Model
```{r,eval=F}
TRAIN3 = TRAIN2 %>% add_residuals(linmod,var="linres")
TEST3 = TEST2 %>% add_residuals(linmod,var="linres")
```
#Part 3: Polynomial Regression Model
##Chunk 1: Fitting Polynomial Regression Models
```{r,eval=F}
poly2mod=lm(W~A+I(A^2),data=TRAIN)
poly3mod=lm(W~A+I(A^2)+I(A^3),data=TRAIN)
poly4mod=lm(W~A+I(A^2)+I(A^3)+I(A^4),data=TRAIN)
anova(linmod,poly2mod,poly3mod,poly4mod,test="Chisq")
```
##Chunk 2: Getting Predictions from Polynomial Models
```{r,eval=F}
TRAIN4 =TRAIN3 %>%
add_predictions(poly2mod,var="poly2pred") %>%
add_predictions(poly3mod,var="poly3pred") %>%
add_predictions(poly4mod,var="poly4pred")
TEST4 =TEST3 %>%
add_predictions(poly2mod,var="poly2pred") %>%
add_predictions(poly3mod,var="poly3pred") %>%
add_predictions(poly4mod,var="poly4pred")
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help