[R] How to plot effect of x1 while controlling for x2
Bert Gunter
gunter.berton at gene.com
Tue Nov 16 00:05:47 CET 2010
... and/or perhaps ?coplot in base R graphics or ?xyplot in trellis.
-- Bert Gunter
On Mon, Nov 15, 2010 at 2:25 PM, Greg Snow <Greg.Snow at imail.org> wrote:
> Look at Predict.Plot (and TkPredict) from the TeachingDemos package.
>
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Mark Na
>> Sent: Monday, November 15, 2010 2:40 PM
>> To: r-help at r-project.org
>> Subject: [R] How to plot effect of x1 while controlling for x2
>>
>> Hello R-helpers,
>>
>> Please see a self-contained example below, in which I attempt to plot
>> the effect of x1 on y, while controlling for x2.
>>
>> Is there a function that does the same thing, without having to
>> specify that x2 should be held at its mean value? It works fine for
>> this simple example, but might be cumbersome if the model was more
>> complex (e.g., lots of x variables, and/or interactions).
>>
>> Many thanks,
>>
>> Mark
>>
>>
>> #make some random data
>> x1<-rnorm(100)
>> x2<-rnorm(100,2,1)
>> y<-0.75*x1+0.35*x2
>>
>> #fit a model
>> model1<-lm(y~x1+x2)
>>
>> #predict the effect of x1 on y, while controlling for x2
>> xv1<-seq(min(x1),max(x1),0.1)
>> yhat_x1<-
>> predict(model1,list(x1=xv1,x2=rep(mean(x2),length(xv1))),type="response
>> ")
>>
>> #plot the predicted values
>> plot(y~x1,xlim=c(min(x1),max(x1)), ylim=c(min(y),max(y)))
>> lines(xv1,yhat_x1)
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
>
--
Bert Gunter
Genentech Nonclinical Biostatistics
More information about the R-help
mailing list