[R] Problem using predict() to draw lines() if predictor variable is logged on the fly
peter dalgaard
pdalgd at gmail.com
Fri Oct 3 00:47:48 CEST 2014
Well, at least the immediate cause is clear:
>> list(log(ArKm00,10)=xx)
is invalid syntax. If you want a list element _named_ log(ArKm00,10), you'll need to quote the name. However, it's not going to work anyway, because that isn't what predict() expects. You don't supply logged variables, you supply the originals and predict() will do the logging.
This is how it does work:
> x <- rexp(10) ; y <- rnorm(x, mean=log(x))
> fit <- lm(y~log(x))
> fit
Call:
lm(formula = y ~ log(x))
Coefficients:
(Intercept) log(x)
0.05337 1.05081
> predict(fit, new=list(x=1))
1
0.05337405
(Notice that since log(1)==0, the predicted value equals the intercept.)
To be more specific, this works through the fact that predict() internally generates the model frame for newdata as, effectively,
> model.frame(delete.response(terms(fit)), list(x=1))
log(x)
1 0
On 02 Oct 2014, at 23:07 , Adams, Jean <jvadams at usgs.gov> wrote:
> You will have better luck getting replies to your post if you provide code
> that we can run. In other words, provide some small example data instead
> of referring to your data frame that we have no access to. You can use the
> output from dput() to provide a subset of your data frame to the list.
>
> dput(mydataframe[1:50, ])
>
> Jean
>
> On Thu, Oct 2, 2014 at 11:12 AM, <mtb954 at gmail.com> wrote:
>
>> Hello,
>>
>> I am plotting glms with logged predictors. I would like to define the
>> logged variables "on the fly" rather than hard coding them into my
>> dataframe.
>>
>> This code (with hard-coded logged variables) works just fine:
>>
>> xx<-seq(-2,.5,by=0.1); lines(xx,predict(power,list(LogArKm00=xx),type=
>> "response"),col="black",lwd=2,lty=1) #LogArKm00 is a variable in my
>> dataframe
>>
>> but my attempt to define them "on the fly" doesn't work (see error below):
>>
>> plot(log(WbAb,10)~log(ArKm00,10),data=dat) #power model
>>
>> xx<-seq(-2,.5,by=0.1); lines(xx,predict(power,list(log(ArKm00,10)=xx),type=
>> "response"),col="black",lwd=2,lty=1) #trying to log the variable "on the
>> fly"
>>
>>
>> Error: unexpected '=' in " lines(xx,predict(power,list(log(ArKm00,10)="
>>
>> I would really appreciate any help sorting this out!
>>
>> Many thanks
>>
>> Mark
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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 at 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list