Displaying 1 result from an estimated 1 matches for "yhat_x1".
2010 Nov 15
3
How to plot effect of x1 while controlling for x2
...s 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)