[R] rms plot.Predict question: swapping x- and y- axis for categorical predictors

Frank Harrell f.harrell at vanderbilt.edu
Tue Oct 30 20:29:37 CET 2012


The new version of rms is now on CRAN (for Mac and Windows probably
tomorrow).  You can now do p <- Predict(...); plot(p, ~ x2, nlines=TRUE,
type='p') to get what you want.

Frank


stephsus wrote
> Hi Frank,
> 
> Thanks for your reply! Using p <- Predict(f, x2); plot(p, ~x2,
> nlines=TRUE)
> makes a categorical predictor (x2, with two levels) look like a continuous
> one. Is there a way to only use dots (error margins), and no continuous
> line between the predictor values (as with p <- Predict(f, x2); plot(p),
> except with the axes reversed)?
> 
> Best,
> --Stephanie
> 
> On Tue, Oct 23, 2012 at 11:20 AM, Frank Harrell [via R] <

> ml-node+s789695n4647187h22 at .nabble

>> wrote:
> 
>> Stephanie,
>>
>> I just realized this is already implemented.  For your problem use p <-
>> Predict(f,x2); plot(p, ~ x2, nlines=TRUE)
>> Frank
>>
>> Frank Harrell wrote
>> Stephanie,
>>
>> I'm working on an option for the plot method for Predict that will allow
>> you to do this.  Note that this approach will not result in very readable
>> predictor category labels when they are long character strings.  If you
>> are
>> using linux I can get you a new version of rms with this option as soon
>> as
>> I've finished it, otherwise I can give you a workaround using source( ).
>>  If you don't hear from me in a few days please write back.
>>
>> Frank
>>
>> stephsus wrote
>> Hello all,
>>
>> I'm trying to plot the effects of variables estimated by a regression
>> model fit individually, and for categorical predictors, the independent
>> variable shows up on the y-axis, with the dependent variable on the
>> x-axis.
>> Is there a way to prevent this reversal?
>>
>> Sample code with dummy data:
>>
>> # make dummy data
>> set.seed(1)
>> x1 <- runif(200)
>> x2 <- sample(c(1,2),200, TRUE)
>> x3 <- sample(c(0,1),200,T)
>> x4 <- runif(200)
>>
>> # the dependent variable:
>> distance <- (x1/3 + x2 + rnorm(200)^2 - x3 - x4/2)
>>
>> # factor two vars, and add to datadist:
>> x3 <- factor(x3)
>> x2 <- factor(x2)
>>
>> d <- datadist(x1,x2,x3,x4)
>> options(datadist="d")
>>
>> # Make a simple model:
>> f <- ols(distance ~ x1 + x2 + x4+ x3, x=T)
>>
>> # plot variable effect of a categorical variable:
>> plot(Predict(f, x2))
>>
>> ^ above step generates a plot with x2 on the y-axis and distance on the
>> x-axis, which is the opposite of what I'm aiming for. The continuous
>> variables do not have this problem; nor does the plot(Predict(f))
>> function
>> to plot all of the effects at once.
>>
>> Thank you so much in advance for your replies! My apologies if this
>> question has been answered already; I've tried searching to no avail.
>>
>> Best,
>> Stephanie
>>
>> (Stanford University, Department of Linguistics)
>>
>>   Frank Harrell
>> Department of Biostatistics, Vanderbilt University
>>
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://r.789695.n4.nabble.com/rms-plot-Predict-question-swapping-x-and-y-axis-for-categorical-predictors-tp4646891p4647187.html
>>  To unsubscribe from rms plot.Predict question: swapping x- and y- axis
>> for categorical predictors, click
>> here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4646891&code=c3RlcGhzdXNAZ21haWwuY29tfDQ2NDY4OTF8MzY4NDA5NzUw>
>> .
>> NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
> 
> 
> 
> -- 
> ____________________________
> 
> Stephanie Sin-yun Shih
> Ph.D. candidate
> Department of Linguistics
> Stanford University

> stephsus@

> http://stanford.edu/~stephsus
> 
> ____________________________





-----
Frank Harrell
Department of Biostatistics, Vanderbilt University
--
View this message in context: http://r.789695.n4.nabble.com/rms-plot-Predict-question-swapping-x-and-y-axis-for-categorical-predictors-tp4646891p4647928.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list