Re: [R] Problem storing lm() model in a list

2010-12-05 Thread Tal Galili
Hi Harold,
I think the error stems from the data.frame you are entering into the
predict function.
Your data.frame must have the EXACT column name as the one used for the
creation of the model object.
Is that the case in your code?


Best,
Tal

Contact
Details:---
Contact me: tal.gal...@gmail.com |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
--




On Sat, Dec 4, 2010 at 9:30 AM, Harold Pimentel haroldpimen...@gmail.comwrote:

 Hi all,

 I recently wrote some code to store a number of polynomial models in a list
 and return this list. The model is returned fine, but when I make a
 subsequent call to predict(), I have an error. The code for polyModel
 selection is listed at the end of the email. Here is an example of the
 error:

  poly.fit - polyModelSelection(x,y,10,F)
  for (d in 1:4) {
 + lm.model - poly.fit$models[[d]]
 + curve(predict(lm.model,data.frame(x)),add=T,lty=d+1)
 + }
 Error: variable 'poly(x, d, raw = T)' was fitted with type nmatrix.1 but
 type nmatrix.10 was supplied

 Does anyone have any ideas?



 Thanks,

 Harold



 

 polyModelSelection - function(x,y,maxd,add.dim=F) {
dim.mult - 0
if (add.dim) {
dim.mult = 1
}
bestD - 1
bestError - 1
loss - 1
lm.models - vector(list,maxd)
for (d in 1:maxd) {
lm.mod - lm(y ~ 0 + poly(x,d,raw=T))
lm.models[[d]] - lm.mod
loss[d] - modelError(lm.mod,data.frame(x),y)+d*dim.mult
if (d == 1){
bestError - loss[d]
}
if (loss[d]  bestError) {
bestError - loss[d]
bestD - d
}
# print(loss[d])
}
list(loss=loss,models=lm.models,best=c(bestD))
 }

 modelError - function(model,x,y) {
sum((y-predict(model,x))^2)
 }
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Problem storing lm() model in a list

2010-12-05 Thread William Dunlap
Your problem arises because predict() is using
the value d=10 whenever it evaluates poly(x,d,raw=TRUE).
The details are that this is the value that d in your
function polyModelSelection had when the function finished.
That value is stored in the environment
   environment(poly.fit$models[[i]]$terms)
for i in 1:10 (which is the same environment for all
10 entries).  It contains the following objects
   objects(envir=environment(poly.fit$models[[1]]$terms))
   [1] add.dim   bestD bestError d
   [5] dim.mult  formula   lm.modlm.models
   [9] loss  maxd  x y
and predict will look there for objects named in
the formula but not found in its newdata= argument.
Use get(d, envir=...) to see that d is 10 in all
of them.

I don't know the best way to avoid the problem.
One way is to put a literal value for degree into your
call to poly(), which also has the advantage that your
printouts show what it is.  In the following I used
substitute() to put the explicit value of degree into
your formula and used do.call when calling lm so the
formula itself (not the name formula) is put into
the call component of the lm object.  This seems kludgy
to me, but I think it gets the job done.

polyModelSelection - function (x, y, maxd, add.dim = F) 
{
dim.mult - 0
if (add.dim) {
dim.mult = 1
}
bestD - 1
bestError - 1
loss - 1
lm.models - vector(list, maxd)
for (d in 1:maxd) {
# next assignment added
formula - substitute(y ~ 0 + poly(x, degree = d, raw = TRUE), 
list(d = d))
# change lm(...) to do.call(lm, list(...))
lm.mod - do.call(lm, list(formula))
lm.models[[d]] - lm.mod
loss[d] - modelError(lm.mod, data.frame(x), y) + d * 
dim.mult
if (d == 1) {
bestError - loss[d]
}
if (loss[d]  bestError) {
bestError - loss[d]
bestD - d
}
}
list(loss = loss, models = lm.models, best = c(bestD))
}

Another way around the problem might be to replace
the environment stored in those terms compononents
with emptyenv().  Then none of the variables in the
function polyModelSelection could be used.  You could
do that with
  for(d in seq_along(poly.fit$models)) {
environment(poly.fit$models[[1]]$terms) - emptyenv()
  }
I like the first was better because I can easily see what
d is.
 
This sort of issue arises whenever we use functions
that use nonstandard evaluation rules, like the modelling
functions, subset(), library(), and help().  They are
easy to call from the top level command line but harder
to deal with when called with variables for arguments.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of Harold Pimentel
 Sent: Friday, December 03, 2010 11:31 PM
 To: r-help@r-project.org
 Subject: [R] Problem storing lm() model in a list
 
 Hi all,
 
 I recently wrote some code to store a number of polynomial 
 models in a list and return this list. The model is returned 
 fine, but when I make a subsequent call to predict(), I have 
 an error. The code for polyModel selection is listed at the 
 end of the email. Here is an example of the error:
 
  poly.fit - polyModelSelection(x,y,10,F)
  for (d in 1:4) {
 + lm.model - poly.fit$models[[d]]
 + curve(predict(lm.model,data.frame(x)),add=T,lty=d+1)
 + }
 Error: variable 'poly(x, d, raw = T)' was fitted with type 
 nmatrix.1 but type nmatrix.10 was supplied
 
 Does anyone have any ideas? 
 
 
 
 Thanks,
 
 Harold
 
 
 ##
 ##
 
 polyModelSelection - function(x,y,maxd,add.dim=F) {
 dim.mult - 0
 if (add.dim) {
 dim.mult = 1
 }
 bestD - 1
 bestError - 1
 loss - 1
 lm.models - vector(list,maxd)
 for (d in 1:maxd) {
 lm.mod - lm(y ~ 0 + poly(x,d,raw=T))
 lm.models[[d]] - lm.mod
 loss[d] - modelError(lm.mod,data.frame(x),y)+d*dim.mult
 if (d == 1){
 bestError - loss[d]
 }
 if (loss[d]  bestError) {
 bestError - loss[d]
 bestD - d
 }
 # print(loss[d])
 }
 list(loss=loss,models=lm.models,best=c(bestD))
 }
 
 modelError - function(model,x,y) {
 sum((y-predict(model,x))^2)
 }
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide 
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Problem storing lm() model in a list

2010-12-05 Thread Harold Pimentel
Hi Tal,

That is correct. In the code, I actually call the same exact data.frame that 
was used to create the object. I understand I can call predict() without a 
data.frame to get this result, but I would like to predict other datasets as 
well.



Thanks,

Harold

On Dec 5, 2010, at 7:18 AM, Tal Galili wrote:

 Hi Harold,
 I think the error stems from the data.frame you are entering into the predict 
 function.
 Your data.frame must have the EXACT column name as the one used for the 
 creation of the model object.
 Is that the case in your code?
 
 
 Best,
 Tal
 
 Contact 
 Details:---
 Contact me: tal.gal...@gmail.com |  972-52-7275845
 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | 
 www.r-statistics.com (English)
 --
 
 
 
 
 On Sat, Dec 4, 2010 at 9:30 AM, Harold Pimentel haroldpimen...@gmail.com 
 wrote:
 Hi all,
 
 I recently wrote some code to store a number of polynomial models in a list 
 and return this list. The model is returned fine, but when I make a 
 subsequent call to predict(), I have an error. The code for polyModel 
 selection is listed at the end of the email. Here is an example of the error:
 
  poly.fit - polyModelSelection(x,y,10,F)
  for (d in 1:4) {
 + lm.model - poly.fit$models[[d]]
 + curve(predict(lm.model,data.frame(x)),add=T,lty=d+1)
 + }
 Error: variable 'poly(x, d, raw = T)' was fitted with type nmatrix.1 but 
 type nmatrix.10 was supplied
 
 Does anyone have any ideas?
 
 
 
 Thanks,
 
 Harold
 
 
 
 
 polyModelSelection - function(x,y,maxd,add.dim=F) {
dim.mult - 0
if (add.dim) {
dim.mult = 1
}
bestD - 1
bestError - 1
loss - 1
lm.models - vector(list,maxd)
for (d in 1:maxd) {
lm.mod - lm(y ~ 0 + poly(x,d,raw=T))
lm.models[[d]] - lm.mod
loss[d] - modelError(lm.mod,data.frame(x),y)+d*dim.mult
if (d == 1){
bestError - loss[d]
}
if (loss[d]  bestError) {
bestError - loss[d]
bestD - d
}
# print(loss[d])
}
list(loss=loss,models=lm.models,best=c(bestD))
 }
 
 modelError - function(model,x,y) {
sum((y-predict(model,x))^2)
 }
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Problem storing lm() model in a list

2010-12-04 Thread Harold Pimentel
Hi all,

I recently wrote some code to store a number of polynomial models in a list and 
return this list. The model is returned fine, but when I make a subsequent call 
to predict(), I have an error. The code for polyModel selection is listed at 
the end of the email. Here is an example of the error:

 poly.fit - polyModelSelection(x,y,10,F)
 for (d in 1:4) {
+ lm.model - poly.fit$models[[d]]
+ curve(predict(lm.model,data.frame(x)),add=T,lty=d+1)
+ }
Error: variable 'poly(x, d, raw = T)' was fitted with type nmatrix.1 but type 
nmatrix.10 was supplied

Does anyone have any ideas? 



Thanks,

Harold




polyModelSelection - function(x,y,maxd,add.dim=F) {
dim.mult - 0
if (add.dim) {
dim.mult = 1
}
bestD - 1
bestError - 1
loss - 1
lm.models - vector(list,maxd)
for (d in 1:maxd) {
lm.mod - lm(y ~ 0 + poly(x,d,raw=T))
lm.models[[d]] - lm.mod
loss[d] - modelError(lm.mod,data.frame(x),y)+d*dim.mult
if (d == 1){
bestError - loss[d]
}
if (loss[d]  bestError) {
bestError - loss[d]
bestD - d
}
# print(loss[d])
}
list(loss=loss,models=lm.models,best=c(bestD))
}

modelError - function(model,x,y) {
sum((y-predict(model,x))^2)
}
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.