[R] problem using do.call and substitute for predict.glm using poly()
Gavin Simpson
gavin.simpson at ucl.ac.uk
Mon Oct 27 20:39:32 CET 2003
Dear Thomas,
Thank you for you reply and solution - it works perfectly! It hadn't
occurred to me that a 1 column subset of a data frame was still a data
frame and that therefore I didn't need to use data.frame() at all!
To the list as well, please ignore my second mail on this thread. I was
trying to clarify my particular problem by focussing on what the problem
actually was, without all the confusing information in the first post.
It was not my intention to 'hassle' the list until someone provided me
with an answer. As always, peoples' time and effort that goes into
helping posters to this list solve their R-related problems is very much
appreciated, and I for one am extremely grateful for the help I have
received from the list over the past few years.
Many thanks,
Gav
Thomas Lumley wrote:
> On Mon, 27 Oct 2003, Gavin Simpson wrote:
>
>
>>But R throws up the following error:
>>
>>Error in poly(Alk1, degree = 2, coefs = structure(list(alpha =
>>c(37.7515662650602, :
>> Object "Alk1" not found
>>
>>When trying to evaluate the following code:
>>
>>pAsgn <- paste("predList[[i]][[n]] <- try(predict(resList$Y$X, newdata
>>= data.frame(X = predData$X), type = 'response', se = TRUE))")
>>pAsgn <- parse(text = pAsgn)[[1]]
>>for (i in namY) {
>> for (n in namX) {
>> TAsgn <- do.call("substitute", list(pAsgn, list(n = n, i
>>= i, X = as.name(n), Y = as.name(i))))
>> eval(TAsgn)
>> }
>>}
>>
>>Alk1 is used above as an example, all 23 predictors are 'not found'
>>depending on which part of the loop I'm in. Investigation of the
>>predList object after this has run shows for example:
>>
>>$Unk.nown$NCR
>>[1] "Error in poly(NCR, degree = 2, coefs = structure(list(alpha =
>>c(218.156626506024, : \n Object \"NCR\" not found\n"
>>attr(,"class")
>>[1] "try-error"
>>
>>pAsgn contains a parsed R expression:
>>
>>predList[[i]][[n]] <- try(predict(resList$Y$X, newdata =
>>data.frame(X = predData$X), type = "response", se = TRUE))
>>
>>I think I have narrowed the problem down to the fact that the first X in
>>newdata = data.frame(X = predData$X)... is not being substitute with the
>>variable in question, where as all the other X and Y's are being
>>substituted:
>
>
> Yes, that's right.
>
>
>>(n and i would be supplied by for loops (see above) so I have
>>substituted two values below as if they had been in the loop)
>>
>> > do.call("substitute", list(pAsgn, list(n = namX[1], i =
>>namY[1], X = as.name(n), Y = as.name(i))))
>>predList[["Acr.harp"]][["Alk1"]] <- try(predict(resList$Unk.nown$NCR,
>> newdata = data.frame(X = predData$NCR), type = "response",
>> se = TRUE)) ^^^^^^ problem here
>>
>>If i supply the values I want for one of the runs, such as:
>>
>> > predList[[1]][[1]] <- try(predict(resList$Acr.harp$Alk1, newdata =
>>data.frame(Alk1 = predData$Alk1), type = "response", se = TRUE))
>>
>>Then this works, so the question is, how to I get X to be substituted in
>>the above call? Perhaps this is not the cause of the error, so if anyone
>>else has other suggestions.
>
>
>
> You should be able to use
> newdata=predData[n]
> which would substitute to
> newdata=predData["Alk1"]
> or even just
> newdata=predData
>
> If you actually needed to substitute the tags you would (I think) need to
> work with the parsed expression directly, which is possible but icky:
>
>
>>names(pAsgn[[3]][[2]][[3]])[2]<-"Alk1"
>>pAsgn
>
> predList[[i]][[n]] <- try(predict(resList$Y$X, newdata = data.frame(Alk1 = predData$X),
> type = "response", se = TRUE))
>
>
> -thomas
>
>
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson [T] +44 (0)20 7679 5522
ENSIS Research Fellow [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC [E] gavin.simpson at ucl.ac.uk
UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/
London. WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list