Error in `mutate()`:Caused by error in `roll_mean_impl()`: ! Not compatible with requested type: [type=list; target=double].

Hi, I have daily 6 stocks prices data. start from Jan 2019 to Dec 2022. I want to split the data set to multiple training sets. if I want to do it manually I will have the following

log.return<-na.omit(Return.calculate(data, method = "log")*100)  #daily log return , this is equal to : r.log <- (log(data) - log(lag(data, k = 1)) ) *100
# generate 1000 random weights

return.1=window(log.return[1:230]) 
return.2=window(log.return[21:251])
return.3=window(log.return[40:271])
return.4=window(log.return[61:291])
return.5=window(log.return[82:312])
return.6=window(log.return[104:334])
return.7=window(log.return[124:354])
return.8=window(log.return[146:376])
return.9=window(log.return[168:398])
return.10=window(log.return[188:419])
return.11=window(log.return[211:440])
return.12=window(log.return[231:462])
return.13=window(log.return[252:482])
return.14=window(log.return[273:504])
return.15=window(log.return[292:523])
return.16=window(log.return[314:542])
return.17=window(log.return[335:564])
return.18=window(log.return[355:586])
return.19=window(log.return[377:606])
return.20=window(log.return[399:628])
return.21=window(log.return[420:649])
return.22=window(log.return[441:671])
return.23=window(log.return[463:692])
return.24=window(log.return[483:713])
return.25=window(log.return[505:734])
return.26=window(log.return[524:756])
return.27=window(log.return[543:776])
return.28=window(log.return[566:795])
return.29=window(log.return[587:818])
return.30=window(log.return[607:838])
return.31=window(log.return[629:859])
return.32=window(log.return[650:880])
return.33=window(log.return[672:900])
return.34=window(log.return[693:923])
return.35=window(log.return[714:944])
return.36=window(log.return[735:965])
return.37=window(log.return[757:986])


I want to split the data using some rolling window function. I have been advised to use the following code

library(dplyr)
library(RccpRoll)

# 'daily_sessions' is the dataset
log.return.df%>%
  mutate(
# Weekly length is the rolling mean of 7 'lengths'
daily_length = roll_mean(length, 230, fill = NA, align = "right")

however I received the following error

Error in `mutate()`:
! Problem while computing `daily_length = roll_mean(log.return.df, 230,
  fill = NA, align = "right")`.
Caused by error in `roll_mean_impl()`:
! Not compatible with requested type: [type=list; target=double].
Run `rlang::last_error()` to see where the error occurred.

Any advice how to solve the problem

I recommend a reprex; to support your issue; see - next paragraph
Also perhaps look into slider package as an alternative to RcppRoll

Thanks for providing code. Could you kindly take further steps to make it easier for other forum users to help you? Share some representative data that will enable your code to run and show the problematic behaviour.

How do I share data for a reprex?

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

this is the code i used to generate my data


data<-NULL
tickers  <- c("^GSPC" ,"^IXIC",'^DJI','EMHY','AGG','TIP') # S&P 500 (^GSPC)stock index, DJI=dow jones stock index, IXIC = nasdac stock index  , AGG=U.S. Aggregate Bond ETF 
data<-NULL
for (ticker in tickers) 
  data <- cbind(data,getSymbols(ticker, from = "2019-01-01", to="2022-12-30", auto.assign = F)[,4]) # we subset closing price column 
colnames(data)<-tickers  # just change closing price name to tickers  name 


# then calculate the log return

log.return<-na.omit(Return.calculate(data, method = "log")*100)  #daily log return , this is equal to : r.log <- (log(data) - log(lag(data, k = 1)) ) *100

the head of log return data that I am interested in is :

> dput(head(log.return))
structure(c(-2.50683315282973, 3.3759398727554, 0.698597599634621, 
0.964858745334585, 0.408967139948579, 0.450824151275331, -3.08400033521199, 
4.1719784080934, 1.24774208017868, 1.07183628245675, 0.86733228472653, 
0.415828388510953, -2.86782306456352, 3.2394397219166, 0.418143813516991, 
1.08245398003195, 0.38463031079452, 0.512942294711038, 0.314324454699699, 
0.625695896049239, 0.444546191582873, 0.132983172678935, 0.331668625415071, 
0, 0.412026049588654, -0.299485477948203, -0.168855571661553, 
-0.0751404179700543, 0.0845240729052676, -0.11271370774697, 0.581819823098773, 
-0.208705673664422, -0.0636036568860021, 0.0817735027784927, 
0.11799139157862, -0.236123076575812), class = c("xts", "zoo"
), src = "yahoo", updated = structure(1677515786.16901, class = c("POSIXct", 
"POSIXt")), ret_type = "log", coredata_content = "logReturn", na.action = structure(1L, class = "omit", index = 1546387200), index = structure(c(1546473600, 
1546560000, 1546819200, 1546905600, 1546992000, 1547078400), tzone = "UTC", tclass = "Date"), dim = c(6L, 
6L), dimnames = list(NULL, c("^GSPC", "^IXIC", "^DJI", "EMHY", 
"AGG", "TIP")))

I want to split this daily return data by using some rolling window function. I want each set size to be 230 daily data.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.