# R: Constrained coefficients for fitting a linear model to a subset of the data

I fit a regression with 37 variables to my entire dataset and got regression results. One of the variable is the distance in miles (dist). I believe that my data follows two distinct regimes with respect to this variable dist. Therefore, I am testing for a structural break at each distance points to maximize a ML function.

For each distance break (0 to 58), I subset the data in two and compute the OLS regression for the paired dataset. The optimal distance break is chosen according to these regression residuals by maximizing a likelihood function (not presented here).

for (i in 1:60){
Chicago.near <- subset(Chicago, loop<=i-1)
Rnear <- lm(data = Chicago.near, log_price ~ as.factor(year) + .... + dist)
Chicago.far <- subset(Chicago, loop>i-1)
Rfar <- lm(data = Chicago.far, log_price ~ as.factor(year) + ... + dist)
}


While this procedure works, it sets freely the other variables in each regression. However, I want to impose the same coefficients on the other 36 variables (coming from the pooled regression).

How to therefore constrain 36 coefficients in my paired regressions?

• This unusual situation makes one wonder what you are trying to achieve and whether the procedure you describe would be a legitimate way to do it. Could you explain? – whuber Jul 21 '17 at 15:37
• The new form of your question sounds similar to stats.stackexchange.com/questions/291436. It has a similar solution: use a suitable spline with a knot at the "distance break." – whuber Jul 26 '17 at 18:12
• Hi, I however need to get 2 regressions models as I need residuals for both models to compute the tests I'm running. If I combine them with a knot at the "distance break" as suggested, I would obtain only one estimate for σ2, right? – Max Jul 27 '17 at 7:16
• That is correct. To model different error variances before and after the break, ideally you would solve the likelihood equations. But there are various ways to get good approximations with the software you have, such as a weighted OLS solution in which the weights are the reciprocals of the estimated error variances from the two separate regressions. Apart from this issue of different error variances, your question is identical to the one asked and answered at stats.stackexchange.com/questions/146959/…: take a look. – whuber Jul 27 '17 at 13:52
• Thank you very much @whuber for your answers. It is much appreciated. I definitely need to look further into the MLE solutions to this problem. I am just too knew to R to implement it now. Thanks again. – Max Jul 31 '17 at 13:17