0

I am usually working in Python, but wanted to perform a Chow Test on time series data, which seems to work much better with R's 'strucchange' library.

The Chow Test works, but when plotting it with 'ggplot2' I get the following error for my code:

"Error in new_data_frame(list(xintercept = xintercept)) : object 'dd' not found"

chows<-plyr::dlply(dat1,~variable,function(dd){
  point=((1:nrow(dd))[dd$PricingDate==as.POSIXct("2021-05-05 15:00:00 UTC")])
  chowtest<-strucchange::sctest(data=dd,value~1,type="Chow",point=point)
       fs<-strucchange::Fstats(data=dd,formula=value~1)
       breaks<-strucchange::breakpoints(value~1,data=dd)$breakpoints
      list(chowtest=chowtest,fs=fs,breaks=breaks)})


breaks<-plyr::ddply(dat1,~variable,function(dd){data.frame(breaks=dd$PricingDate[strucchange::breakpoints(value~1,data=dd)$breakpoints])})


graph<-ggplot(data=dat1,aes(x=PricingDate,y=value))+
  geom_line()+
  geom_vline(xintercept=dd$PricingDate[point],color="red",alpha=.5,size=3)+
  geom_vline(data=breaks,aes(xintercept=breaks))+
  facet_wrap(~variable,scales="free")

It might be an easy fix, but I don't quite understand why it can't fetch dd from above or how I can fix it so that it gives me the red bar I want.

Any help highly appreciated, thanks!

max0506
  • 53
  • 6
  • 1
    If I'm correct You are using dd only as parameter in function, so you can only use it in the function body, not in `geom_vline()`. – KacZdr Jul 23 '21 at 16:25
  • Try changing to `geom_vline(xintercept=dat$PricingDate[point], color="red",alpha=.5,size=3)` – MrFlick Jul 23 '21 at 16:26
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jul 23 '21 at 16:26

0 Answers0