[18814] in s-news-athena

home help back first fref pref prev next nref lref last post

RE: [S] predict function

daemon@ATHENA.MIT.EDU (Buttrey, Samuel)
Mon Aug 2 12:30:21 1999

Message-Id: <DB17FBA383E4D211A9AF00A0C99E0169CAD9B6@monterey.nps.navy.mil>
From: "Buttrey, Samuel" <sebuttre@monterey.nps.navy.mil>
To: "'Lionel Siau'" <cyls98@btinternet.com>, S-news@wubios.wustl.edu
Date: Mon, 2 Aug 1999 09:30:12 -0700 
Mime-Version: 1.0
Content-Type: text/plain


fan@katan.cybercom.net and Lionel Siau between them raise an interesting
point. To answer the first question, if you're going to use a matrix for
your "new.data" in predict, then it should match up exactly with what the
original matrix would have looked like. In fan@katan's example:
	a <-1:10
	b<-a+rnorm(10)
	mod <- lm(b~a)
# what's the prediction when x=11?
	predict(mod, 11) # this doesn't work for me at all
	predict (mod, c(1, 11)) # adding the intercept explicitly produces
what you need.

Better:
	predict (mod, data.frame (a = 11)) # supplying the names gets rid of
the need for the intercept

To answer Lionel's question, yes, *in linear models* you need supply only
the required columns. But in trees, you apparently need to supply a data
frame with all the original columns -- even if those column aren't actually
used in building the tree. For example, consider this data frame in which
only c will be used to model d:

> test <- data.frame (a = rnorm (10), b = rexp (10), c = 1:10, d = 1:10 +
rnorm (10, sd = .3))
> test.tree <- tree (d ~ ., data = test)
> summary (test.tree)
...
Variables actually used in tree construction:
[1] "c" ...

So I should be able to predict using just c, I might have thought. But no.
> predict (test.tree, data.frame (c = 2:6))
Error in predict.tree(test.tree, data.frame(c = 2:6)): Length of variable 1
is 10 != length of row names (5)
Dumped

If I make up and a and b and stick them in there, it works:

> predict (test.tree, data.frame (c = 2:6, a = letters[1:5], b =
letters[1:5]))
       1       2       3       4        5 
 2.95272 2.95272 2.95272 2.95272 8.012537

> predict (test.tree, data.frame (c = 2:6, a = letters[1:5], b =
letters[1:5]))
       1       2       3       4        5 
 2.95272 2.95272 2.95272 2.95272 8.012537

> predict (test.tree, data.frame (c = 2:6, a = 100:104, b = log(100:104)))
       1       2       3       4        5 
 2.95272 2.95272 2.95272 2.95272 8.012537
> 

How 'bout that?

Sam Buttrey
buttrey@nps.navy.mil
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post