问题
The code is self contained, except the datasets which is linked below.
.csv files used in the code, download this first please: https://drive.google.com/?authuser=0#folders/0B1ciW4R5hjUCRFpjQlJKZGFqcVU
library(midasr)
library(zoo)
yvellaregdata <- read.table("~/Desktop/attempt1/ymonthlyjackson.csv", quote="\"")
yvellareg <- ts(yvellaregdata, start=c(2008,7), frequency=12)
xvellareginit <- read.table("~/Desktop/attempt1/xdailyjackson.csv", quote="\"")
xvellaregzoo <- zoo(xvellareg)
xvellareg <- as.numeric(xvellaregzoo) #i had to convert to numeric for it to work
#yvellareg is the monthly y variable
#xvellareg is the daily x variable
betareg <- midas_r(yvellareg ~ mls(yvellareg, 1, 1) + mls(xvellareg, 3:25, 30), start=NULL)
summary(betareg)
#Defining data for forecasting
xdailyfulldataread <- read.table("~/Desktop/attempt1/xdailyfulldatajackson.csv", quote="\"")
xdailyfulldata <- zoo(xdailyfulldataread)
xdailyfulldata <- as.numeric(xdailyfulldata)
ymonthlyfulldataread <- read.table("~/Desktop/attempt1/ymonthlyfulldatajackson.csv", quote="\"")
ymonthlyfulldata <- ts(ymonthlyfulldataread,start=c(2008,7), frequency=12)
fulldata <- list(xx=xdailyfulldata,
yy=ymonthlyfulldata)
insample <- 1:length(yvellareg)
outsample <- (1:length(fulldata$yy))[-insample]
#errorhere
avgf<-average_forecast(list(betareg),
data=fulldata,
insample=insample,
outsample=outsample)
sqrt(avgf$accuracy$individual$MSE.out.of.sample)
回答1:
Since you already prepared the data with in-sample and full-sample outside of R, there is no need to convert it to time series objects.
Here is the cleaned-up version of your code, which assumes that data files are in R working directory:
library(midasr)
yvellareg <- scan("ymonthlyjackson.csv")
xvellareg <- scan("xdailyjackson.csv")
#yvellareg is the monthly y variable
#xvellareg is the daily x variable
betareg <- midas_r(yvellareg ~ mls(yvellareg, 1, 1) + mls(xvellareg, 3:25, 30), start=NULL)
summary(betareg)
#Defining data for forecasting
xdailyfulldata <- scan("xdailyfulldatajackson.csv")
ymonthlyfulldata <- scan("ymonthlyfulldatajackson.csv")
fulldata <- list(xvellareg=xdailyfulldata,
yvellareg=ymonthlyfulldata)
insample <- 1:length(yvellareg)
outsample <- (1:length(fulldata$yvellareg))[-insample]
#errorhere
avgf<-average_forecast(list(betareg),
data=fulldata,
insample=insample,
outsample=outsample)
sqrt(avgf$accuracy$individual$MSE.out.of.sample)
But this still throws an error, since your data is not conformable. Package midasr expects that each low frequency period has the same number of high frequency periods. In your case this is 30. But we have
> length(xdailyfulldata)
[1] 1230
> length(ymonthlyfulldata)
[1] 42
> 1230/42
[1] 29.28571
Since 42*30=1260
it seems you have more monthly than daily observations. Dropping one monthly observation makes the code run without the errors:
fulldata <- list(xvellareg=xdailyfulldata,
yvellareg=ymonthlyfulldata[-42])
insample <- 1:length(yvellareg)
outsample <- (1:length(fulldata$yvellareg))[-insample]
#errorhere
avgf<-average_forecast(list(betareg),
data=fulldata,
insample=insample,
outsample=outsample)
sqrt(avgf$accuracy$individual$MSE.out.of.sample)
[1] 1.118709
来源:https://stackoverflow.com/questions/23165621/error-when-forecasting-with-midasr-reproducible-example-included