Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

python - Is mean of rolling window R2 is the total R2 of regression?

Quick Quesiton here.

I have to calculate the mean of a 60 days rolling window R2 time serie. My time serie has 4680 observations.

I was wondering, is the mean of the serie of R2 the total R2 of the regression?

Example :

Mean(series of r2) = R2 of the regression on all the sample data

question from:https://stackoverflow.com/questions/65905697/is-mean-of-rolling-window-r2-is-the-total-r2-of-regression

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just try it. We show two equivalent ways to define R squared in this context:

library(zoo)
set.seed(123)
n <- 4680
w <- 60
x <- rnorm(4680)

r2 <- function(x) summary(lm(x ~ seq_along(x)))$r.squared
# r2 <- function(x) cor(x, seq_along(x))^2  # this is equivalent

mean(rollapplyr(x, w, r2))
## [1] 0.4992133

r2(x)
## [1] 0.0001151601

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...