On 2012-Jun-18, at 13:11 , Lali wrote:
>
> I am currently working on some quantile regressions using the lmsqreg package and I was wondering if autoplot will work with this object class (lmsqreg.fit); which of course doesn't right off the bat, unless I define the method. The problem is that I am not an R expert and I have no idea how to define the method. I read this R bloggers article but a lot of it is just way over my head. Does anyone have any suggestions on how to go about this?
I have some still-in-development code to fortify output from rq models (quantile regression with the package quantreg, which seems to be the "classic" one). From then on, you would have to write the corresponding autoplot function (which is really:
1. calling fortify
2. calling ggplot() + geom_path() with the appropriate columns from the output of fortify
Hopefully, this, one day, will be in a package.
I paste the below:
fortify.rq <- function(model, data=model$model, ...) {
#
# Extract data from a requession quantile object into a data.frame
#
fitted <- predict(model, ...)
resid <- residuals(model)
# merge with data
res <- data.frame(data, .fitted=fitted, .resid=resid, row.names=NULL)
# identify quantile
res$.tau <- model$tau
res$.tau <- as.factor(res$.tau)
return(res)
}
fortify.rqs <- function(model, data=model$model, ...) {
#
# Extract data from a requession quantile object into a data.frame
#
suppressPackageStartupMessages(require("reshape2", quietly=TRUE))
# get fitted values for all quantiles
fit <- as.data.frame(predict(model))
# convert to long shape
fit <- melt(fit, measure.vars=1:ncol(fit),
variable.name=".tau",
value.name=".fitted")
# get residuals
resid <- as.data.frame(residuals(model))
resid <- melt(resid, measure.vars=1:ncol(resid),
variable.name=".tau",
value.name=".resid")
# merge with fit
fit$.resid <- resid$.resid
# identify quantiles
fit$.tau <- model$tau[as.numeric(fit$.tau)]
fit$.tau <- factor(fit$.tau)
# merge with data
res <- data.frame(data, fit, row.names=NULL)
return(res)
}
Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel:
+33 04 93 76 38 04
Mob:
+33 06 21 05 19 90
http://jo.irisson.com/
Send me large files at:
http://jo.irisson.com/upload/