I am having a problem with the window function in R.
newdata1 <-window(mergedall,start=c(as.Date(as.character("2014-06-16"))),end=c(as.Date(as.character("2015-01-31"))))
I got this error. I am trying to understand how I can fix this issue. Thank you!
Error in window.default(mergedall, start = c(as.Date(as.character("2014-06-16"))), :
'start' cannot be after 'end'
In addition: Warning message:
In window.default(mergedall, start = c(as.Date(as.character("2014-06-16"))), :
'end' value not changed`
I know it's an old post. But, please make sure that "mergedall" is a time series object which was created using the ts command.
While creating the time series object from any vector or series,
some_result_ts <- ts(vector,frequency=xx,start=c(yyyy,m))
This kind of error comes when yyyy is lesser than the start you are specifying in window command.
For example if you take a data frame column or a vector or series , and during the ts formation with ts command, give yyyy=2010,m=1 with a frequency of 12 and assuming it's a 36 month data, the implicit end will be 2013,12.
some_result_ts <- ts(vector,frequency=12,start=c(2010,1))
Then, while using a window function, if you are specifying let's say, start = c(2014,1) , then R will give a message that => 'start' cannot be after 'end' and end value not changed.
Again it's an old post. But since I stumbled upon it by searching the same error. I want to still provide something useful for future Googlers.
I could not replicate your issue because you did not provide your own mergedall dataset. So I am starting with a toy example to show a few places where the problem might be. It's really not that difficult at all.
Potential problem #1:
You did not create a ts object to begin with. Window function operates on a ts object, and it cannot just be a vector took directly from a df. Use ts function to make a vector a ts object first. And then assign it with proper start, end, frequency.
all <-seq(1:8) #eight observations in sequence
Assign these eight values as monthly observations, starting from 201406 to 201501. Frequency 12 means monthly.
all.ts <- ts(all, start = c(2014,6), end = c(2015,1), frequency = 12)
Potential problem #2:
You perhaps already assigned your mergedall series as a ts object, but with different start/end/frequency. My example above was based on monthly observations. So even though they are correct examples, they will not match with your daily-based window function. Window function and the ts object needs to be consistent.
Following my example, the window function would look like:
newdata1 <-window(all.ts,start=c(2014,6),end=c(2015,1) )
Hi here is what you can try, perhaps this would be the solution as I also faced the same problem.
You might not be referring to proper index value in the timeseries object.
In below code I have added the index (i) you can put 1 in case the object has only one series or any number or pass different values using a simple loop.
Hope it helps.!
newdata1 <-window(mergedall[i],start=c(as.Date(as.character("2014-06-16"))),end=c(as.Date(as.character("2015-01-31"))))
I am also a future googler and none of the answers helped me. This was my problem and solution:
MWE issue:
set.seed(50)
data <- ts(rnorm(100), start(1850))
data.train <- window(data, start = 1850, end = 1949)
MWE solution:
set.seed(50)
data <- ts(rnorm(100), start = (1850))
data.train <- window(data, start = 1850, end = 1949)
Issue was the missing equals sign when setting the start date.
The resulting variable data was still a time series; but the give-away was: "Time-Series from 1 to 100" rather than "Time-Series from 1850 to 1949", which told me that something was awry with creating the time series.
The ts function doesn't raise this as an error, presumably because it accepts the start() function from the {stats} package, according to the ?ts doc.
This is probably an issue arising from the format of your 'mergedall' object.
Make sure that you have a ts, xts or a zoo object.
Try f.ex. the following first, in order to ensure the format of your object:
str(mergedall)
Related
I want to use window function to subset a time series. However, the function excludes the date I input as end argument.
window(ts1, end = "2018-09-24")
I couldn't find any argument to change this behavior. Any thought?
The problem arose because of comparing two different types of data, Date and POSIXct.
I solved the issue by finding the indexes of the rows that are after that date and then excluded them from the dataset:
evaluation_date <- "2018-09-24"
indexes_removed <- which(as.numeric(as.Date(index(ts1))) > as.numeric(as.Date(evaluation_date)))
ts1 <- ts1[[-indexes_removed]
I am pretty new to R. So this is a data of 183 columns and multiple rows. I am trying to do a batch forecasting however, I got the error message saying:
"Error in Raw.Data_timeseries_forecast[, i] <- forecast(Raw.Data_timeseries_fit)$mean :
number of items to replace is not a multiple of replacement length"
Could anyone help me to take a look at it?
Thanks!
Raw.Data[is.na(Raw.Data)]<-0
library(forecast)
Raw.Data_timeseries<-msts(Raw.Data[,-1],seasonal.periods = c(7,12,365.25),start=1/1/2014)
ns<-ncol(Raw.Data_timeseries)
h<-365
Raw.Data_timeseries_forecast<-matrix(nrow=h,ncol=ns,byrow =FALSE)
for (i in 1:ns)
{
Raw.Data_timeseries_fit<-stlf(Raw.Data_timeseries[,i])
Raw.Data_timeseries_forecast[,i]<-forecast(Raw.Data_timeseries_fit)$mean
}
write.csv(Raw.Data_timeseries_forecast,"rawdata_stlf.csv")
The issue is that (as far as i can tell, an example of what Raw.Data looks like would help clear it up) is that your line of code :
Raw.Data_timeseries_fit<-stlf(Raw.Data_timeseries[,i])
actually returns a ts object, with a length equal to the whole original Time-series (which I assume is longer than 365 days). You then plug that into the forcast() function, which will output another ts object that is of the original length. However you then try to plug that ts object into the matrix column that has only 365 rows, and thats why it is throwing the "number of items to replace is not a multiple of replacement length" error.
looking at the documentation of the forecast function, you see that it can take both a ts and a model. Looking in the same documentation at the stlf function you see that it is actually a function that creates a stl model and then performs a forecast, so you don't actually need to call:
Raw.Data_timeseries_forecast[,i]<-forecast(Raw.Data_timeseries_fit)$mean
or you could call stlm() instead of stlf and then proceed to call forecast afterwords. Either way however, I'm pretty sure the root problem is in the mismatch between the number of rows of the forecast matrix and the number of observations in the original time series object.
Take a look at the h parameter inside the forecast function, it is returning 2 times your time series length, is that what you want? If no define that explicitly.
You could also solve that problem storing the result into a list:
Raw.Data_timeseries_forecast<-list()
for (i in 1:ns)
{ # i=1
Raw.Data_timeseries_fit<-stlf(Raw.Data_timeseries[,i])
Raw.Data_timeseries_forecast[[i]]<-forecast(Raw.Data_timeseries_fit)$mean
}
Raw.Data_timeseries_forecast_f <- t(do.call("rbind",Raw.Data_timeseries_forecast))
#write.csv(Raw.Data_timeseries_forecast,"rawdata_stlf.csv")
I want to create a Time Series data frame by doing this:
x <- xts(data$length,data$Time.Elapsed)
Then, I got a warning message:
Error in xts(data$length, data$Time.Elapsed) :
order.by requires an appropriate time-based object
So, I was thinking the problem is my "Time.Elapsed" is numeric data. Then I want to convert the data type of "Time.Elapsed", how can I achieve that?
>data$Time Elapsed
Time Elapsed
0
1
2
3
4
5
I want to create a time series data frame, so I need to have a time-based object in R. Here, "Time Elapsed" is a numeric variable (those numbers represent seconds); how can I convert it to time type "seconds"? I searched the Data-time conversion function, like: as.POSIX* {base} But I don't think this function suits my case. Anyone can help me about this? Thank you very much!
I believe you're not going low-level enough on this. xts provides some convenience functions to help determine if you can convert something to xts or not.
xtsible(data) #Will probably tell you it fails with your current setup.
xts builds on zoo, and zoo is a bit more flexible though harder to work with.
library(zoo)
zooData <- zoo(data$length, data$Time.Elapsed)
xtsible(zooData) #Will probably tell you it's ok, but probably doesn't matter since
#most/all of xts's functions work on zoo objects.
xtsData <- xts(zooData)
require(lubridate)
x <- as.POSIXct(strptime(data$Time.Elapsed, format = "%S"))
as.duration(x)
This should do the trick.
I assumed the following code
date = as.Date('2015-05-30')
timeseries = xts()
timeseries[date] = 1
should assign the value of 1 to a date '2015-05-30'. However, it gives me an error
Error in xts(rep(NA, length(index(x))), index(x)) :
order.by requires an appropriate time-based object
What is the proper way to assign the value to an empty xts object?
Thanks,
Vladimir
I think you misunderstand the purpose of the [<-.xts function. You're asking to replace the value at date "2015-05-30" with 1, but your xts object has no data, so there's nothing to replace. What are you actually trying to accomplish?
If you want to insert, you should call rbind(xts(1, as.Date('2015-05-30')), timeseries).
And you should heed Mike Wise's wise advice: it is very inefficient to grow objects like this.
Try something like this:
d1 <- rep(1,21)
d2 <- seq(as.Date("2001-01-01",tz="GMT"),as.Date("2021-01-01",tz="GMT"),length.out=21)
xtsdat <- as.xts(d1,d2)
If you need to build it up row by row, then build the individual vectors that way and form the xts at the end.
I am able to get the following code to work:
world_dat <- get_ensemble_temp(world,"annualavg",2080,2100)
but I would like to change it to historical and start in 1920,1939 (or even earlier). Unfortunately it keeps saying unused arguments
world_dat2 <- get_historical_temp(world,"annualavg",1920,1939)
I basically want to create a world map showing historical temperatures. Any help will be greatly appreciated. Thx!
The reason why you get the "unused argument" error is because the arguments for these two functions are different:
get_ensemble_temp(locator, type, start, end)
get_historical_temp(locator, time_scale)
For the "get_historical_temp" function, you would set time_scale="year", and then subset to the years that you want. E.g.:
USA_dat <- get_historical_temp("USA", "year")
USA_dat_small <- subset(USA_dat, year >= 1920 & year <= 1939,
select=c(1:length(USA_dat)))
The outputs of these functions are quite different, too. You will have to average and summarize the data from "get_historical_temp" to make them comparable to the output of "get_ensemble_temp"
Also, I couldn't get your first line to work with the argument "world."
According to the docs (http://cran.r-project.org/web/packages/rWBclimate/rWBclimate.pdf)
you have to use a vector of all country codes in order to get the whole world's data all at once.