[R-SIG-Finance] Example code for nonparametric estimation of time-varying beta

Mark Knecht markknecht at gmail.com
Sun Aug 22 02:47:11 CEST 2010


2010/8/20 김상환 <skim3 at naver.com>:
> OK! Here are some codes to help you understand my previous questions:
>
> library(mgcv)
> library(tseries)
>
> # download some test data from YAHOO
> spc <- get.hist.quote(instrument = "^gspc", start = "2000-01-01", quote = "Close")
> ibm <- get.hist.quote(instrument = "ibm", start = "2000-01-01", quote = "AdjClose")
> t30 <- get.hist.quote(instrument = "^TYX", start = "2000-01-01", quote = "Close")
>
> RM <- (diff(log(spc)) - t30 / 365)*100.0 # let's say this is excess market return
> R_IBM <- (diff(log(ibm)) - t30 / 365)*100.0 # individual stock's excess return
> t <- 1:length(RM) # time trend
>
> output <- gam(R_IBM ~ RM + s(t, by=RM)) # the code in the paper
> plot(output, rug=F, ylab="Time-Varying Beta") # beta looks two low
>
> output <- gam(R_IBM ~ s(t, by=RM))
> plot(output, rug=F, ylab="Time-Varying Beta")
> # now reasonable but I want time-axis like 2000 2001 ... 2010
>
> Any help would be appreciated!
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>

Just a quick note to point out that your code didn't run for me.

- Mark

> library(mgcv)
This is mgcv 1.6-1. For overview type `help("mgcv-package")'.
> library(tseries)
Loading required package: quadprog
Loading required package: zoo

    ‘tseries’ version: 0.10-22

    ‘tseries’ is a package for time series analysis and computational
    finance.

    See ‘library(help="tseries")’ for details.

Warning messages:
1: package 'tseries' was built under R version 2.11.1
2: package 'zoo' was built under R version 2.11.1
>
> # download some test data from YAHOO
> spc <- get.hist.quote(instrument = "^gspc", start = "2000-01-01", quote = "Close")
Error: object 'spc' not found
> ibm <- get.hist.quote(instrument = "ibm", start = "2000-01-01", quote = "AdjClose")
Error: object 'ibm' not found
> t30 <- get.hist.quote(instrument = "^TYX", start = "2000-01-01", quote = "Close")
Error: object 't30' not found
>
> RM <- (diff(log(spc)) - t30 / 365)*100.0 # let's say this is excess market return
Error: object 'RM' not found
> R_IBM <- (diff(log(ibm)) - t30 / 365)*100.0 # individual stock's excess return
Error: object 'R_IBM' not found
> t <- 1:length(RM) # time trend
Error: object 'lt' not found
>
> output <- gam(R_IBM ~ RM + s(t, by=RM)) # the code in the paper
Error: object 'output' not found
> plot(output, rug=F, ylab="Time-Varying Beta") # beta looks two low
Error in plot(output, rug = F, ylab = "Time-Varying Beta") :
  object 'output' not found
>
> output <- gam(R_IBM ~ s(t, by=RM))
Error: object 'output' not found
> plot(output, rug=F, ylab="Time-Varying Beta")
Error in plot(output, rug = F, ylab = "Time-Varying Beta") :
  object 'output' not found
> # now reasonable but I want time-axis like 2000 2001 ... 2010
>



More information about the R-SIG-Finance mailing list