Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hofnerb/b383a9f0d47e66ba779c to your computer and use it in GitHub Desktop.
Save hofnerb/b383a9f0d47e66ba779c to your computer and use it in GitHub Desktop.
## R version 3.2.2 (2015-08-14) -- "Fire Safety"
## Copyright (C) 2015 The R Foundation for Statistical Computing
## Platform: x86_64-pc-linux-gnu (64-bit)
##  
## R is free software and comes with ABSOLUTELY NO WARRANTY.
## You are welcome to redistribute it under certain conditions.
## Type 'license()' or 'licence()' for distribution details.
##  
##   Natural language support but running in an English locale
##  
## R is a collaborative project with many contributors.
## Type 'contributors()' for more information and
## 'citation()' on how to cite R or R packages in publications.
##  
## Type 'demo()' for some demos, 'help()' for on-line help, or
## 'help.start()' for an HTML browser interface to help.
## Type 'q()' to quit R.

curr <- read.table('EXC.ASC', header=FALSE, dec = ".")
colnames(curr) <- c("Canada", "German Mark", "French Fr", "Pound", "Yen")
summary(curr)
##      Canada        German Mark      French Fr          Pound       
##  Min.   :0.9682   Min.   :1.597   Min.   : 3.969   Min.   :0.4096  
##  1st Qu.:1.1213   1st Qu.:1.875   1st Qu.: 4.505   1st Qu.:0.4933  
##  Median :1.2001   Median :2.319   Median : 5.660   Median :0.5639  
##  Mean   :1.1951   Mean   :2.266   Mean   : 5.913   Mean   :0.5758  
##  3rd Qu.:1.2943   3rd Qu.:2.548   3rd Qu.: 6.905   3rd Qu.:0.6542  
##  Max.   :1.4274   Max.   :3.424   Max.   :10.480   Max.   :0.9445  
##       Yen       
##  Min.   :121.2  
##  1st Qu.:196.9  
##  Median :233.6  
##  Mean   :224.9  
##  3rd Qu.:257.4  
##  Max.   :306.4  

set.seed(1234)

curr1 <- curr
curr1[,-5] <- scale(curr1[,-5])
fout1 <- lm(Yen ~ .,data=curr1)
library(regtools)
## Loading required package: FNN
xdata <- preprocessx(curr1[,-5],25,xval=TRUE)
kout <- knnest(curr1[,5],xdata,25)
ypredknn <- knnpred(kout,xdata$x)
cor(ypredknn,curr1[,5])^2
## [1] 0.9817131
 
library(mboost)
## Loading required package: parallel
## Loading required package: stabs
## This is mboost 2.5-0. See ‘package?mboost’ and ‘news(package  = "mboost")’
## for a complete list of changes.

trnidxs <- sample(1:761,500)
predidxs <- setdiff(1:761,trnidxs)
mbout <- glmboost(Yen ~ .,data=curr1[trnidxs,])
mstop(mbout) <- 10000
### cross-validation does not work for large n small p as no overfitting occurs
# cvr <- cvrisk(mbout, grid = 1:10000)
# plot(cvr)
# mstop(mbout) <- mstop(cvr)

lmout <- lm(Yen ~ .,data=curr1[trnidxs,])
mbpred <- predict(mbout,curr1[predidxs,])
lmpred <- predict(lmout,curr1[predidxs,])
predy <- curr1[predidxs,]$Yen
mean(abs(predy-mbpred))
## [1] 13.4263
mean(abs(predy-lmpred))
## [1] 13.4263

coef(mbout, off2int = TRUE)
##   (Intercept)        Canada `German Mark`   `French Fr`         Pound 
##    224.517080     -4.819203     59.177071    -36.465684     -4.874037 
coef(lmout)
##   (Intercept)        Canada `German Mark`   `French Fr`         Pound 
##    224.517080     -4.819203     59.177071    -36.465684     -4.874037 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment