[R] Time-varying correlation calculation
timh at insightful.com
Tue Jan 23 20:26:35 CET 2007
You can do this using an interaction of bs(time) and x, e.g.:
# Time-varying coefficient
x <- rnorm(100)
time <- ppoints(100)
y <- sin(time) + cos(time)*x + rnorm(100)/10
fit <- lm(y ~ bs(time, df=5) * x)
# Plot estimated intercept vs time
plot(time, coef(fit) + bs(time, df=5) %*% coef(fit)[2:6])
# Plot estimated slope vs time
plot(time, coef(fit) + bs(time, df=5) %*% coef(fit)[8:12])
Thanks to Trevor Hastie for suggesting this approach, when
I made a similar query on S-news years ago.
> I'm interested in getting a series of time-varying correlation, simply between two random variables.
> Could you please introduce a package to do this task?
> Thank you so much for any help.
| Tim Hesterberg Senior Research Scientist |
| timh at insightful.com Insightful Corp. |
| (206)802-2319 1700 Westlake Ave. N, Suite 500 |
| (206)283-8691 (fax) Seattle, WA 98109-3044, U.S.A. |
| www.insightful.com/Hesterberg |
Download S+Resample from www.insightful.com/downloads/libraries
More information about the R-help