Skip to content

Instantly share code, notes, and snippets.

@timcdlucas
Created February 15, 2016 17:48
Show Gist options
  • Save timcdlucas/4c44c0b4fe59592d385a to your computer and use it in GitHub Desktop.
Save timcdlucas/4c44c0b4fe59592d385a to your computer and use it in GitHub Desktop.
Compare slopes bewteen two datasets.
# Make up some data
data1 <- data.frame(length = rnorm(20, 8), weight = 1:20)
data2 <- data.frame(length = rnorm(30, 8), weight = 1:30)
# add a dummy variable and combine the data
data1$dummy <- 'data1'
data2$dummy <- 'data2'
combinedData <- rbind(data1, data2)
# Fit model that ignores the different data sets
model1 <- lm(length ~ weight, data = combinedData)
# Now fit an interaction between the dummy and the slope and coeficients.
model2 <- lm(length ~ dummy*weight, data = combinedData)
library(lmtest)
lrtest(model1, model2)
# If you wanted to ONLY compare the slopes you could do
model3 <- lm(length ~ weight + dummy:weight, data = combinedData)
lrtest(model1, model3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment