# Empirical Finance Lecture 4 Analysis (Part 2)
# Author: Chris Hansman
# Email: [email protected]
# Date : 28/01/21
# Installing Packages
#install.packages(glmnet)
#install.packages(glmnetUtils)
#Loading Libraries
library(glmnet)
library(glmnetUtils)
library(tidyverse)
set.seed(050187)
##
# Loading Data
#-
# Training with 100 Observations as a Tibble
train_100<-read_csv(“training_100.csv”)# Test with 100 Observations as a Tibbletest_100<-read_csv(“test_100.csv”)#————————————————–## Linear Model#————————————————–##————————————————–## Estimating Linear Model on 100 Observationslm_fit_5 <- lm(y~x1+x2+x3+x4+x5, data=train_100)# Predicting Out of Sample and Computing MSEy_pred_5<-predict(lm_fit_5, newdata=test_100)mean((test_100$y-y_pred_5)^2)# Estimating Linear Model on 100 Observationslm_fit <- lm(y~.-y, data=train_100)# Predicting Out of Sample and Computing MSEy_pred_lm<-predict(lm_fit, newdata=train_100)mean((train_100$y-y_pred_lm)^2)# Predicting Out of Sample and Computing MSEy_pred_lm<-predict(lm_fit, newdata=test_100)mean((test_100$y-y_pred_lm)^2)#————————————————–## Lasso Without Cross Validation#————————————————–##Lasso With no Cross Validationglmnet_fit <- glmnet(y~.,data=train_100)glmnet_fit#Coefficients for a Fixed Value of lambdacoef(glmnet_fit, s=1)# Plotting All Coefficients as Lambda Changesplot(glmnet_fit, xvar = “lambda”)# Out of SampleMean Squared Error with lambda=0.2y_pred_glm <- predict(glmnet_fit, s=0.2, newdata=test_100)mean((test_100$y-y_pred_glm)^2)#————————————————–##Lasso With Cross Validation#————————————————–#cv_glmnet_fit <- cv.glmnet(y~.,data=train_100)# Plotting Mean Squared Error as Lambda Changesplot(cv_glmnet_fit)#Lambda That Minimizescv_glmnet_fit$lambda.mincv_glmnet_fit$lambda.1se# Coefficients for lambda.min Estimatecoef(cv_glmnet_fit, s=”lambda.min”)# Coefficients for lambda.1se Estimatecoef(cv_glmnet_fit, s=”lambda.1se”)# Out of Sample Mean Squared Error y_pred_cv <- predict(cv_glmnet_fit, s=”lambda.1se”, newdata=test_100)mean((test_100$y-y_pred_cv)^2)#————————————————–##Lasso With Cross Validation#————————————————–##Loading Data# Training with 200 Observations as a Tibblementi_train<-read_csv(“menti_200.csv”)# Training with 200 Observations as a Tibblementi_test<-read_csv(“menti_200_test.csv”)cv_menti <- cv.glmnet(y ~ . , data=menti_train)# Plotting Mean Squared Error as Lambda Changesplot(cv_menti)#Lambda That Minimizescv_menti$lambda.1se# Coefficients for lambda.min Estimatecoef(cv_menti, s=”lambda.min”)# Coefficients for lambda.1se Estimatecoef(cv_menti, s=”lambda.1se”)# Out of Sample Mean Squared Error y_pred_cv <- predict(cv_menti, s=”lambda.min”, newdata=menti_test)mean((menti_test$y-y_pred_cv)^2)
Reviews
There are no reviews yet.