I am having trouble understanding the xyplot() function to create plots in R. Below, I have an example of R code that does create a nice plot
install.packages("mice")
library("mice")
data <- airquality[, c("Ozone", "Solar.R")]
# Applies regression imputation to Ozone w.r.t Solar.R
imp <- mice(data, method = "norm.predict", seed = 1,
m = 1, print = FALSE)
xyplot(imp, Ozone ~ Solar.R)
The above code creates this image as desired:
code1 output
But the code below does not create a nice plot and instead gives me the error message "Error in UseMethod("xyplot") :
no applicable method for 'xyplot' applied to an object of class "data.frame""
airquality2 <- tidyr::fill(airquality, Ozone)
xyplot(airquality2, Ozone ~ Day)
Why does this occur? I am confused as applying the typeof() function to both "imp" and "airquality2" return "list", so I believe I have the object types correct. Thank you!
Remember that in R, generic functions call a specific method depending on the "class" attribute of the object passed as the first argument. This is known as S3 dispatch. The "class" of an object is not the same thing as its storage mode or internal type, which is what typeof returns. The fact that typeof(imp) == typeof(airquality2) is therefore irrelevant.
xyplot is a generic function, borrowed from the lattice package. The lattice package itself only defines methods for the classes formula and ts. It has no method for data frames.
The reason why xyplot works with imp passed as the first argument is that imp is an object of class "mids", and the method xyplot.mids is defined as an (unexported) function in mice, so there is an available method for it.
The upshot is that, since a method is available for objects of class “formula”, you can easily plot airquality2 by passing the formula as the first argument:
xyplot(Ozone ~ Day, airquality2)
Or explicitly naming the data argument:
xyplot(data = airquality2, Ozone ~ Day)
Both of which result in:
I'm using the RMOA package for R to implement a Hoeffding Tree stream classifier with holdout evaluation.
Everything is training correctly, except when I try to evaluate my model from my held-out test stream I'm getting hit with the following error message:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('HoeffdingTree', 'MOA_classifier', 'MOA_model')"
Having checked the answer to this question, the problem may stem from the fact that the predict() method exists in both the stats and RMOA packages. I have tried to use the :: notation in order to specify which package, but I can't seem to point to RMOA predict(). I also tried uninstalling stats altogether but it hasn't helped.
Does anyone know how to point directly to RMOA's predict(), or is my issue caused by something else entirely?
My R code is below. I'm simply streaming the iris data set for just now, and extracting the first 30 stream items to use for holdout evaluation.
holdout<-function(){
require("RMOA")
#Initialise streams
stream<-datastream_dataframe(iris)
test<-stream$get_points(n=30)
test<-datastream_dataframe(test)
#Specify model
mymodel<-HoeffdingTree(numericEstimator = "GaussianNumericAttributeClassObserver")
#Record execution time for training
start_time<-Sys.time()
while(!stream$finished)
{
mymodel <<- trainMOA(model=mymodel, formula = Species ~ Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data=stream)
}
end_time<-Sys.time()
time_taken <- end_time - start_time
cat("Finished training. Elapsed time: ", time_taken)
#Empty vector to store individual accuracy results of holdout stream elements
accuracies<-c()
#Record the execution time of holdout evaluation
start_time<-Sys.time()
while(!test$finished)
{
samp<-test$get_points(n=1)
pred <- predict(mymodel, samp, type="response")
}
end_time<-Sys.time()
time_taken <- end_time - start_time
cat("Finished training. Elapsed time: ", time_taken)
}
the predict method in package RMOA is an internal variable you can call it like this:
RMOA:::predict.MOA_trainedmodel
full example:
library(RMOA)
data(iris)
stream <- datastream_dataframe(iris)
test <- stream$get_points(n = 30)
test <- datastream_dataframe(test)
mymodel <- HoeffdingTree(numericEstimator = "GaussianNumericAttributeClassObserver")
mymodel <- trainMOA(model = mymodel, formula = Species ~ Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data = stream)
in my case the predict function is not masked (which is odd if it is not exported):
pred1 <- predict(mymodel, iris, type = "response")
but if it was I could use:
pred2 <- RMOA:::predict.MOA_trainedmodel(mymodel, iris, type = "response")
and the result would be the same:
all.equal(pred1, pred2)
#output
TRUE
I checked the NAMESPACE of RMOA and predict function is exported but for some reason
RMOA::predict.MOA_trainedmodel
results in
Error: 'predict.MOA_trainedmodel' is not an exported object from
'namespace:RMOA'
while
RMOA:::predict.MOA_trainedmodel
does not
I am using the package ROBETS in R to make forecast. I need to re-train my model on a extended time series. Below is the MWE:
library(robets)
ts.train <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841), start=c(20001, 1), frequency=12)
model.robets <- robets(ts.train)
ts.train.dev <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841,830,812,160,238,53,760), start=c(20001, 1), frequency=12)
model.robets.retrain <- robets(ts.train.dev, model=model.robets)
I get the following error:
Error in robetsTargetFunctionInit(par = par, y = y, errortype = errortype, :
k Problem!
An easy solution to your problem is adding the argument use.initial.values = TRUE. This argument states that the same initial values are used for model.robets as for model.robets.retrain. That makes sense, since by default the initial values are estimated in a short startup period, which is the same for both time series.
The solution:
library(robets)
ts.train <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841), start=c(20001, 1), frequency=12)
model.robets <- robets(ts.train)
ts.train.dev <- ts(c(60,209,51,34,208,64,122,99,82,194,136,177,110,332,300,151,128,206,129,92,164,814,1286,826,893,949,1014,830,877,605,773,870,1610,970,1192,1222,466,1963,841,830,812,160,238,53,760), start=c(20001, 1), frequency=12)
model.robets.retrain <- robets(ts.train.dev, model=model.robets, use.initial.values = TRUE)
However, the error you describe should not happen. Therefore I changed the default setting, and I solved the bug you found. A new version of robets will soon appear on CRAN.
Good morning,
I am currently using a time series of daily sales to make forecasting.
The dataset, called myts has previously been transformed into time series object.
Whenever I run the following code, it gives me an error:
require(tsDyn)
x <- log(myts)
mod.ar <- linear(x, m=2)
Error: x must be a vector, not a ts object, do you want
stats::lag()?
Best regards,
Alex
The problem reported by Alessandro can be generated by the lag function of dplyr which overrides the lag function of stats.
Try this:
detach("package:dplyr", unload=TRUE)
library(tsDyn)
linear(log(lynx), m=2)
Here linear works correctly giving:
Non linear autoregressive model
AR model
Coefficients:
const phi.1 phi.2
2.4352150 1.3842377 -0.7477757
Now, try this:
detach("package:tsDyn", unload=TRUE)
library(dplyr)
library(tsDyn)
linear(log(lynx), m=2)
The code gives the error message:
Error: `x` must be a vector, not a ts object, do you want `stats::lag()`?
Try this example (notice that I start with a vector and then convert it to a time series object)
require(tsDyn)
set.seed(1234)
tsdatav <- (seq(1:300)+rnorm(300,1000,10))
myts <- ts(tsdatav, frequency = 365, start = c(2017, 6))
plot(myts)
x <- log(myts)
mod.ar <- linear(x, m = 2)
mod.ar
Yes, the problem is caused by dplyr having its own version of lag, which overrides the usual lag function, as well as a mistake in the way package tseriesChaos (which tsDyn depends on) imports stas::lag. A fix has been sent and approved, but not yet submitted to CRAN:
https://github.com/antoniofabio/tseriesChaos/commit/8abcc5a2d6d65588cdcec5527d4e5cb96eeccaec
In the meanwhile, you can simply overwrite lag back:
lag <- stats::lag
This should now work:
mod.ar <- linear(lynx, m=2)
And if you really want dplyr's version, use dplyr::lag
`
I have following R code which does not work when trying to pre-whiten other series by the model generated for the other series.
-- Libraries;
library(forecast);
library(TSA);
library(xts);
-- Read from csv;
....
-- Do transforms;
Power=xts(data1[2],seq(from=as.Date("2011-01-01"), to=as.Date("2013-09-18"),by="day"),frequency=7);
Temp=xts(data2[1],seq(from=as.Date("2011-01-01"), to=as.Date("2013-09-18"),by="day"),frequency=7);
-- Prewhiten for CCF;
mod1=Arima(Temp,order=c(2,0,1),seasonal=list(order=c(1,1,1)));
Box.test(mod1$residuals,lag=365,type=c("Ljung-Box"));
x_series=mod1$residuals;
y_filtered=residuals(Arima(Power,model=mod1));
Last Part does not work since I get error:
Error in stats::arima(x = x, order = order, seasonal = seasonal, include.mean = include.mean, :
wrong length for 'fixed'
What goes wrong here?
Arima and stats::arima both require ts objects. The error is caused by xts objects being used. Try this instead:
Power <- ts(data1[2], frequency=7)
Temp <- ts(data2[1], frequency=7)
mod1 <- Arima(Temp,order=c(2,0,1),seasonal=c(1,1,1))
Box.test(residuals(mod1),lag=365,type=c("Ljung-Box"))
x_series <- residuals(mod1)
y_filtered <- residuals(Arima(Power,model=mod1))