Peter Flom
2003-Dec-19 15:19 UTC
[R] Question re labels in r-part (continuation of a thread from a while back)
Hello again
I have modeled a tree using rpart, with the DV being a log
transformation of the variable I am really interested in (I transformed
the DV due to extreme skewness). By default, text.rpart labels the
nodes with the value of yval, which in this case is not what I want; I'd
like the labels to be on the original metric, but label in text.rpart
requires a "column name of x$frame", and the original DV is not on
that
frame.
So, I tried the following:
{create the tree}
tr.totpart <- rpart(log(totpart +1) ~ sexfact + age + windle +
eabused
+ as.factor(pabau) + positive + controlling + lenient +
druguse.ever
+ anycsw,
xval = 10, cp = 0.000000001, data = duhray)
{prune the tree}
tr.totpart.pruned <- prune (tr.totpart, cp = .02)
{ad the original metric to the frame}
meanpart <- exp(tr.totpart.pruned$frame$yval)-1
tr.totpart.pruned$frame <- cbind(tr.totpart.pruned$frame, meanpart)
and thought this was good because summary(tr.totpart.pruned$frame)
lists meanpart as one of the columns.
BUT when I tried
plot(tr.totpart.pruned, compress = T, uniform = T, nspace = .5, margin
= .1)
text(tr.totpart.pruned, splits = T, all = T, pretty = 0, digits = 4,
use.n = T, fancy = T, label = meanpart)
I got an error
Error in text.rpart(tr.totpart.pruned, splits = T, all = T, pretty = 0,
:
Label must be a column label of the frame component of the
tree
Any thoughts or insights or help appreciated, as always
Peter
PS Given the recent posts on the thread about how to ask questions, I
should perhaps add that I have looked thru ?rpart, and through Atkinson
& Therneau (1997): An Introduction to recursive partitioning using the
RPART routines
Peter L. Flom, PhD
Assistant Director, Statistics and Data Analysis Core
Center for Drug Use and HIV Research
National Development and Research Institutes
71 W. 23rd St
www.peterflom.com
New York, NY 10010
(212) 845-4485 (voice)
(917) 438-0894 (fax)
Thomas W Blackwell
2003-Dec-19 15:27 UTC
[R] Question re labels in r-part (continuation of a thread from a while back)
Peter - (Just gessing about the structure of rpart objects ...) How about tr.totpart.pruned$frame <- cbind(tr.totpart.pruned$frame, meanpart = exp(tr.totpart.pruned$frame$yval)-1) This appends meanpart as an additional column of tr.totpart.pruned$frame. After this step, I would expect the plot command below to work. I haven't tried this out myself, though. - tom blackwell - u michigan medical school - ann arbor - On Fri, 19 Dec 2003, Peter Flom wrote:> Hello again > > I have modeled a tree using rpart, with the DV being a log > transformation of the variable I am really interested in (I transformed > the DV due to extreme skewness). By default, text.rpart labels the > nodes with the value of yval, which in this case is not what I want; I'd > like the labels to be on the original metric, but label in text.rpart > requires a "column name of x$frame", and the original DV is not on that > frame. > > So, I tried the following: > > {create the tree} > tr.totpart <- rpart(log(totpart +1) ~ sexfact + age + windle + > eabused > + as.factor(pabau) + positive + controlling + lenient + > druguse.ever > + anycsw, > xval = 10, cp = 0.000000001, data = duhray) > > > > {prune the tree} > tr.totpart.pruned <- prune (tr.totpart, cp = .02) > > > {ad the original metric to the frame} > > > meanpart <- exp(tr.totpart.pruned$frame$yval)-1 > tr.totpart.pruned$frame <- cbind(tr.totpart.pruned$frame, meanpart) > > > and thought this was good because summary(tr.totpart.pruned$frame) > lists meanpart as one of the columns. > > BUT when I tried > > > plot(tr.totpart.pruned, compress = T, uniform = T, nspace = .5, margin > = .1) > text(tr.totpart.pruned, splits = T, all = T, pretty = 0, digits = 4, > use.n = T, fancy = T, label = meanpart) > > I got an error > > Error in text.rpart(tr.totpart.pruned, splits = T, all = T, pretty = 0, > : > Label must be a column label of the frame component of the > tree > > > > > > Any thoughts or insights or help appreciated, as always > > Peter > > PS Given the recent posts on the thread about how to ask questions, I > should perhaps add that I have looked thru ?rpart, and through Atkinson > & Therneau (1997): An Introduction to recursive partitioning using the > RPART routines > > > > Peter L. Flom, PhD > Assistant Director, Statistics and Data Analysis Core > Center for Drug Use and HIV Research > National Development and Research Institutes > 71 W. 23rd St > www.peterflom.com > New York, NY 10010 > (212) 845-4485 (voice) > (917) 438-0894 (fax) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >