# How to make linear regression model while 'control' confounding variables?

I have data from survey, and Trying to build a linear regression model using R like A~ B however, want to control C, D, E, F, G. like Age, Sex, and other confounding variables. I tried to make some models using lm, glm, etc. and fitted it to my data.

However, in Multivariate regression models, I cannot get the graph like below. To use 'segmented' function of R to cut-off point, I guess, I should able to get estimated equation between A and B, while all other variables are controlled.

The image above describes what I want to do. linear model between A and B, but actual model includes confounders C, D, F, and somehow'controlled' them. The author described that

fitted the two linear regression models for high B and low B, and calculated the sums of squares of residuals (=observed A -estimated A), from the two models for each B. The models with the lowest residual sums of squares were the best models.

It seems to be drawn from R(and author said so), but I cannot find any good approach.

I'm afraid this question seems to 'tool specific' question/ If so, I'll amend this question, and update it with any help I got.

Thanks.

• As long as you don't define what you mean by "want to just 'control' variables" you won't get an answer to your question. From the figure caption in the (unsprecified) publication you are referring to it should be possible to deduce how they "controlled" the covariates. – Roland Dec 4 '13 at 15:13

Nothing unique to R was done for graph. From my reading they a priori decided on the cut-point, which makes everything easy. If the cut-point was data based, then it would be more painful, but fairly easy with segmented package in R.

Note: This isn't what they did, but what they should have done. The results should be identical. This appears to be a standard piecewise linear model, that they have described in an awkward way.

Assuming 10 divides low B from high B, generate new variable b_high. b_high =0 if B<10, b_high=B-10 if B>10.
Stata

regress A B b_high C D F
# to generate slope for b>10
lincom B+b_high


or R

fit=lm(A~ B + b_high C D F


coefficient for b+b_high= slope of line b>10, b=slope of line B<10. Then you just fit predicted values of A onto a scatter plot of A B.

• @charls, Thanks, but unfortunately, it's from actual dataset. containing A,B,C,D,E. I did segmented function using, fit=lm(A~B+C+E), and o<-segmented(fit, seg.Z=~B,psi=10). Now I got very weird result that the segment point is less than the min value of B. maybe the 'psi' part are wrong, but i have no idea to fix it... – CattusNsLuna Dec 4 '13 at 14:30
• (1) you can do it by self-code: r-bloggers.com/… (2) Change the psi values. I'd try 50, 40, 30,and 20. segmented can be start point sensitive. (3) try with fit=lm(A~B) to get starting values. (4) try another package ? SiZeR – charles Dec 4 '13 at 15:32