I have some data:
> xt2
# A tibble: 5 x 3
# Groups: symbol [1]
symbol wavgd1 rowNo
<chr> <dbl> <int>
1 REGI 4.84 2220
2 REGI 0.493 2221
3 REGI -0.0890 2222
4 REGI 0.190 2223
5 REGI -1.93 2224
which, when I process it with lm():
xt2t = lm( formula=wavgd1~rowNo, data=as.data.frame(xt2) )
gives the expected result (fitted.values[5] is the test here):
> summary(xt2t)
Call:
lm(formula = wavgd1 ~ rowNo, data = as.data.frame(xt2))
Residuals:
1 2 3 4 5
1.3723 -1.5937 -0.7907 0.8733 0.1388
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3078.1707 979.5475 3.142 0.0516 .
rowNo -1.3850 0.4408 -3.142 0.0516 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.394 on 3 degrees of freedom
Multiple R-squared: 0.7669, Adjusted R-squared: 0.6892
F-statistic: 9.87 on 1 and 3 DF, p-value: 0.05159
But when I process it using rollapply:
xl = zoo::rollapply(xt2,
width=5,
FUN = function(Z)
{
print( as.data.frame(Z) )
t = lm( formula=wavgd1~rowNo, data=as.data.frame(Z) )
print( summary(t) )
return( t$fitted.values[[5]] )
},
by.column=FALSE,
align="right",
fill=NA)
it returns me the input data:
[1] NA NA NA NA -1.929501
Call:
lm(formula = wavgd1 ~ rowNo, data = as.data.frame(Z))
Residuals:
ALL 5 residuals are 0: no residual degrees of freedom!
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.844 NA NA NA
rowNo2221 -4.351 NA NA NA
rowNo2222 -4.933 NA NA NA
rowNo2223 -4.654 NA NA NA
rowNo2224 -6.773 NA NA NA
Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: NaN
F-statistic: NaN on 4 and 0 DF, p-value: NA
In the rollapply() case it looks like each row is being processed as an individual case rather than en masse?