# Empirical Finance Lecture 4 Analysis (Part 1)
# Author: Chris Hansman
# Email: [email protected]
# Date : 28/01/21
# Loading Libraries
library(tidyverse)
# Reading Data
poly <- read_csv(“polynomial.csv”)# Plotting Dataggplot(aes(x=x,y=y), data=poly)+geom_point(aes(color=test)) +theme_classic()# Test and Training Datapoly_train <-poly %>%
filter(test==train)
poly_test <- poly %>%
filter(test==test)
# Linear Fit
ggplot(data=poly_train, aes(x = x, y = y)) +
theme_classic() +
geom_point(color=red)+
geom_smooth(method = lm, formula = y ~ x, se=F, color=black)
# Quadratic Fit
ggplot(data=poly_train, aes(x = x, y = y)) +
theme_classic() +
geom_point(color=red)+
geom_smooth(method = lm, formula = y ~ x+I(x^2), se=F, color=black)
# Quadratic Fit with Poly
ggplot(data=poly_train, aes(x = x, y = y)) +
theme_classic() +
geom_point(color=red)+
geom_smooth(method = lm, formula = y ~ poly(x,2, raw=TRUE), se=F, color=black) +
geom_smooth(method = lm, formula = y ~ poly(x,40, raw=TRUE), se=F, color=purple)+
geom_point(data=poly_test,color=blue)
#Quadratic Regression Model
ols_quad <- lm(y~poly(x,2, raw=TRUE), data=poly_train)#Prediction on Training Datapoly_train <- poly_train %>%
mutate(yhat_quad = predict(ols_quad)) %>%
mutate(pe_quad=yhat_quad-y)
#Mean Squared Error
mse_train_quad <- mean((poly_train$pe_quad)^2) #MSE#Prediction on Testing Datapoly_test <- poly_test %>%
mutate(yhat_quad = predict(ols_quad, newdata=poly_test)) %>%
mutate(pe_quad=y-yhat_quad)
#Mean Squared Error out of Sample
mse_test_quad <- mean((poly_test$pe_quad)^2)#Comparing MSEmse_train_quadmse_test_quad#High Order Regression Modelols_25 <- lm(y~poly(x,25,raw=TRUE), data=poly_train)yhat_train_25 <-predict(ols_25)mse_train_25 <- mean((yhat_train_25-poly_train$y)^2)# Model Fitmse_train_quadmse_train_25#High Order Out of Sample Fityhat_test_25 <-predict(ols_25, newdata=poly_test)mse_test_25 <- mean((yhat_test_25-poly_test$y)^2)#Comparing MSemse_test_quadmse_test_25
Reviews
There are no reviews yet.