CS计算机代考程序代写 finance # Empirical Finance Lecture 4 Analysis (Part 2)

# Empirical Finance Lecture 4 Analysis (Part 2)
# Author: Chris Hansman
# Email: chansman@imperial.ac.uk
# 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 Tibble test_100<-read_csv("test_100.csv") #--------------------------------------------------# # Linear Model #--------------------------------------------------# #--------------------------------------------------# # Estimating Linear Model on 100 Observations lm_fit_5 <- lm(y~x1+x2+x3+x4+x5, data=train_100) # Predicting Out of Sample and Computing MSE y_pred_5<-predict(lm_fit_5, newdata=test_100) mean((test_100$y-y_pred_5)^2) # Estimating Linear Model on 100 Observations lm_fit <- lm(y~.-y, data=train_100) # Predicting Out of Sample and Computing MSE y_pred_lm<-predict(lm_fit, newdata=train_100) mean((train_100$y-y_pred_lm)^2) # Predicting Out of Sample and Computing MSE y_pred_lm<-predict(lm_fit, newdata=test_100) mean((test_100$y-y_pred_lm)^2) #--------------------------------------------------# # Lasso Without Cross Validation #--------------------------------------------------# #Lasso With no Cross Validation glmnet_fit <- glmnet(y~.,data=train_100) glmnet_fit #Coefficients for a Fixed Value of lambda coef(glmnet_fit, s=1) # Plotting All Coefficients as Lambda Changes plot(glmnet_fit, xvar = "lambda") # Out of Sample Mean Squared Error with lambda=0.2 y_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 Changes plot(cv_glmnet_fit) #Lambda That Minimizes cv_glmnet_fit$lambda.min cv_glmnet_fit$lambda.1se # Coefficients for lambda.min Estimate coef(cv_glmnet_fit, s="lambda.min") # Coefficients for lambda.1se Estimate coef(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 Tibble menti_train<-read_csv("menti_200.csv") # Training with 200 Observations as a Tibble menti_test<-read_csv("menti_200_test.csv") cv_menti <- cv.glmnet(y ~ . , data=menti_train) # Plotting Mean Squared Error as Lambda Changes plot(cv_menti) #Lambda That Minimizes cv_menti$lambda.1se # Coefficients for lambda.min Estimate coef(cv_menti, s="lambda.min") # Coefficients for lambda.1se Estimate coef(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)