Skip to content

Instantly share code, notes, and snippets.

@wabarr
Last active September 8, 2015 15:18
Show Gist options
  • Save wabarr/52931e8577f7772d3910 to your computer and use it in GitHub Desktop.
Save wabarr/52931e8577f7772d3910 to your computer and use it in GitHub Desktop.
correlation between two random walk time series, that happen to be trended

TimeSeries

Andrew Barr
July 6, 2015

create totally random time series

ts1 <- cumsum(rnorm(1000))
ts2 <- cumsum(rnorm(1000))
par(mfrow=c(1,2))
plot(ts1, type="l")
plot(ts2, type="l")

plot values of ts1 by ts2 and to regression

par(mfrow=c(1,1))
plot(ts2~ts1, pch=16)

summary(lm(ts2~ts1))
## 
## Call:
## lm(formula = ts2 ~ ts1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -19.4187  -5.5092  -0.8079   5.5828  18.7287 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 14.18006    0.43241   32.79   <2e-16 ***
## ts1         -0.91694    0.01183  -77.54   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.449 on 998 degrees of freedom
## Multiple R-squared:  0.8576,	Adjusted R-squared:  0.8575 
## F-statistic:  6012 on 1 and 998 DF,  p-value: < 2.2e-16

difference data

plot(diff(ts2)~diff(ts1), pch=16)

summary(lm(diff(ts2)~diff(ts1)))
## 
## Call:
## lm(formula = diff(ts2) ~ diff(ts1))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9254 -0.7226  0.0105  0.6817  3.3244 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.074338   0.032598   2.280   0.0228 *
## diff(ts1)   -0.006398   0.031551  -0.203   0.8393  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.028 on 997 degrees of freedom
## Multiple R-squared:  4.124e-05,	Adjusted R-squared:  -0.0009617 
## F-statistic: 0.04112 on 1 and 997 DF,  p-value: 0.8393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment