Reputation: 2825
I'm trying to plot a cox proportional hazard model in R. (or a logit model) I used the following code (which I copied from https://sites.google.com/site/daishizuka/toolkits/plotting-logistic-regression-in-r)
c<-coxph(formula=Surv(year, promo)~prov.yrs, data=cul)
curve(predict(c, data.frame(prov.yrs=x), type="risk"), add=TRUE)
I get the error message
Error in plot.xy(xy.coords(x, y), type = type, ...) :
invalid graphics state
I believe there is something wrong with plotting this, so I was wondering if there is a way to plot this. I get the same error message when I use glm. Any help will be appreciated!!
Upvotes: 0
Views: 2030
Reputation: 263342
The error message suggests you did not have a device open or perhaps there was some other problem with the plot you were trying to add to? That code produces a plot over a range input [0,1] with a toy example I built from the coxph
help page. Perhaps your range for the 'prov.yrs' is different than an existing plot, or there is no device open? Try plot.new()
, plot whatever else you were going to use, and then rerun? (The add=TRUE will suppress plotting of the box, axes and labels.)
Upvotes: 0
Reputation: 49640
The example you copied from shows a logistic regression, but you are fitting a coxph model, they are very different in how they are handled.
If you just want a plot of the the hazard ratio then your code will basically work (except you are adding to a plot that is not there, which may be what generates the error, try changing add
to FALSE
).
If you want to plot the survival curve(s) then use the survfit
function to get the predicted survival information and plot that.
Upvotes: 2