-1

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):

enter image description 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?

Ian
  • 1,507
  • 3
  • 21
  • 36
  • Please read the instructions t the top of the [tag:r] tag page. – G. Grothendieck Nov 18 '20 at 19:33
  • You can't have a mixture of numeric and non-numeric columns. Passing xt2[, -1] might work but until you provide the input as dput output as discussed in the instructions at the top of the [tag:r] tag we can't tell exactly what you have. – G. Grothendieck Nov 19 '20 at 12:45
  • Thanks for that. I was unsure what in the instructions I had failed to provide. But I tried it with slider instead and that works, as I've written up below, so I'm going forward with that instead. Again, thanks for your help. – Ian Nov 19 '20 at 13:04
  • The input needs to be provided using `dput`. – G. Grothendieck Nov 19 '20 at 13:10

1 Answers1

-1

Not sure why rollapply() is being petulant here, but I switched to using slide:: and all is well:

f5 = function(x) { 
    r = lm( formula=wavgd1~rowNo, x )
    return( r$fitted.values[[length(r$fitted.values)]] ) 
  }

mutate( xt, rval=slide_dbl(xt, ~f5(.x), .before = 4, .complete=TRUE) )
Ian
  • 1,507
  • 3
  • 21
  • 36