0

I have the following vector

s<-c(-0.1516865068,0.0777125437,  0.0596115942, -0.0066893552,  0.0111096953,   
0.0523087458, -0.0376922037,  0.0640068468,  0.0085058973, -0.0059950522,
0.0889039984,  0.0242030489, -0.0777979006, -0.0091988501,  0.0744002004, -0.0343007491,   
0.0156983014, -0.0601026480,  0.0848964025, -0.1040045470,0.0328945035, -0.0400064460, 
-0.0545073955, -0.0214083450, -0.0816092944,  0.1197897561,  0.0709888066,-0.0589121429, 
-0.0020130924, -0.0519140419,0.0015850086, -0.0215159408, -0.0315168903, -0.0645178398,  
0.0136812107, -0.0961197388,  0.0024793117,  0.0549783622,  0.1498774128,  0.0256764633,
-0.0124244862,  0.0529745643, -0.0709263852, -0.0438273347, -0.0442282842, 
-0.0109292336, -0.0118301831,  0.0290688674,  0.1202679179, -0.0881330316,-0.0685339811, 
-0.1182349306, -0.0772358800,  0.1094631705, -0.0542377790, -0.0561387285,  
0.0630603220,  0.0307593725,  0.0441584230,  0.0601574736, -0.0956434759, -0.0228444254,   
0.1257546251,  0.0437536756,  0.1038527261, -0.1235482234,  0.0090508272,  0.0865498777, 
-0.0672510718, -0.0219520213,-0.0088529708, -0.0123539203,  0.0323451303, -0.0395558192,  
0.0880432313, 0.0126422818,  0.1011413323, -0.0223596172, -0.1004605667, 
-0.0113615161,-0.0436624656, -0.0006634151,  0.0170356354,  0.0376346859, 0.0266337364,  
0.0501327869, -0.0153681625, -0.0671691120,  0.0525299385,  0.0332289890,-0.0551719605,  
0.0532270900,  0.0184261405, -0.0312748089,  0.0636242416, -0.0742767079, -0.0419776574,  
0.0608213931, -0.0559795564,  0.0298194941)

I have a function

spectrum(s, method = c("ar"))

I would like to run on a rolling window different values of width on the above function using rollapply or sapply from zoo package.

rollapply(s, width= ,FUN= , by =  )

I guess I have to write the function applying sapply similar as below but I can not find a way to put toguether the function and the parameters of sapply toguether

wide<-c(4,6,12)
for(i in 1:length(wide)){
b<-spectrum(s, method = c("ar"))
c<-sapply(s, width=wide[i],FUN=b, by = 1)
Sp[i] <- c    
}

Error en FUN(X[[1L]], ...) : unused arguments (width = 4, by = 1)

When doing the above I get the below as I not find a way to pass the arguments

Thank you

Barnaby
  • 1,472
  • 4
  • 20
  • 34
  • `Z` is never used. Also please read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – G. Grothendieck Apr 09 '14 at 16:31
  • Edditing the question now – Barnaby Apr 09 '14 at 16:31
  • I can't reproduce the error you get using `rollapply`. On the `sapply` call, "width" and "by" are not arguments neither in `sapply` nor in `are`. But, I still don't get what you're trying to do, to be honest. If you want to use "width" and "by" in your `sapply` call these have to be arguments of `are`. – alexis_laz Apr 09 '14 at 17:23
  • `X` is never used within your function. Should it be `ar <- spectrum(X, method = c('ar')`? – maloneypatr Apr 09 '14 at 17:56
  • Appologies made some significant eddits to the question as it was not thought enough and unclear – Barnaby Apr 09 '14 at 18:04

1 Answers1

1

Is this what you are trying to do?

ar <- function(x){ spectrum(x, method = 'ar') }
rollapply(s, width = 12, FUN = ar)

It might be better to use lapply, iterating over the indices, as rollapply seems to lose all the structure output by spectrum

ar <- function(x){ spectrum(x, method = 'ar') }
lapply(1:(length(s)-11), function(i) ar(s[i:(i+11)]) )

I'm sure there's a neater way of doing that though.

Russ Hyde
  • 2,154
  • 12
  • 21
  • Yes in principle, Thank you, though would like to pass several width= numbers so I can compute all of them at the same time having sets of different results – Barnaby Apr 09 '14 at 18:13
  • Sorry, I didn't see that in your original post. I assumed you wanted to fix the errors first – Russ Hyde Apr 09 '14 at 18:16