# Comparing beta coefficient of same variable from two different models in R

Let assume I have a data with 100 rows and 3 variables (y,x1,x2). Now, I fit a regression model with the FIRST 70 observations & get the coefficients as b11 & b12 respectively for x1 & x2.

Then I fit another regression model with the remaining 30 observations & get b21 & b22 as the coefficient for x1 & x2 respectively. What I want to be assist is ... how can I compare b11 & b21(or b12 & b22) using R. There is "anova" option in R but that compares different model techniques with same data.

• Compare what exactly? You can use anova(model1,model2) to compare the two models as a whole. Dec 18 '18 at 9:19
• you can extract a fitted linear model's coefficients with coef(fit) Dec 18 '18 at 9:29
• What do you mean by comparing? Models that are fitted to different chunks of the data set are not directly comparable in terms of explaining the entire dataset. However, you can make predictions using both models and see which one is more predictive. It is better to check with the Cross Validated community for this type of questions. Dec 18 '18 at 9:40
• Would you please post a link to the data? Dec 18 '18 at 11:10

## 1 Answer

The example below shows how to fit 2 groups of data with a single linear model using the factor set. You can then do statistical tests (see summary and anova) to check if there is a significant difference in their coefficients. In this case, the generated data groups have the same y-intercept, but different slopes with respect to x.

# generate synthetic dataset ----------------------------------------------
set.seed(1)
n <- 140
df <- data.frame(x = seq(n), set = factor(rep(c("a","b"), each=n/2)))

# same intercept, but different slope coefs for the sets (plus error added)
df$y <- c(10, 10)[df$set] + df$x*c(1.3, 1.6)[df$set] + rnorm(n, sd = 10)

plot(y ~ x, df, col = df\$set)


# fit models --------------------------------------------------------------

# model including different intercepts and slopes for both sets
fit0 <- lm(y ~ x + set + x:set, data = df)
summary(fit0)

# one could remove the intercept shift for setb
fit1 <- lm(y ~ x + x:set, data = df)
summary(fit1)

anova(fit0, fit1) # not significantly different, so take the simpler fit1 model