# How to check whether or not two regression curves from different datasets are different in R

Here is an example code:

set.seed(3)

data1 = iris[sample(c(1:dim(iris)[1]), 30), ]
data2 = iris[sample(c(1:dim(iris)[1]), 50), ]

model1 = lm(Petal.Length ~ log(Petal.Width),
data = data1)
model2 = lm(data2$$Petal.Length ~ log(data2$$Petal.Width))

par(mfrow = c(1, 4))
plot(data1$$Petal.Width, data1$$Petal.Length)
points(sort(data1$$Petal.Width), predict(model1, newdata = data1[order(data1$$Petal.Width), ]),
col = "red",
type = "l")

plot(data2$$Petal.Width, data2$$Petal.Length)
points(sort(data2$$Petal.Width), predict(model1, newdata = data2[order(data2$$Petal.Width), ]),
col = "red",
type = "l")

summary(model1)
summary(model2)


As in the code, I fitted two different curves from two different datasets using the simple regression method in which the explanatory variable is in logarithm form. Like in the code, the datasets have the same variables with different size.
I want to check that the two curves (model1 and model2) are statistically different or not.
How can I do it in R?

---- edited ---- I know that there are many questions about comparing regression lines, but my case is a different one from previous ones because I want to compare different models built from different data sets while other cases are to compare different models built from the same data set.

Let me state more about the context of my purpose to elaborate on the term "compare."
I made a model (the curve) for an experimental site and want to apply this model to many other sites (over the country). I acknowledge that it would not proper to apply the model to other sites where the model is not based on (the resulted curve of another site may be different from the previous one). So, I made one more curve for another study site and trying to compare the two models built from different sites.
This is why the two data sets have the same variable list. And the common points of the two data sets in the example can be ignored in term of my purpose.

Thank you!

• You still have lines -- in log-petal-width. The previous posts you refer to that are about comparing lines apply directly. Aug 19, 2019 at 8:32
• @Glen_b Thank you for your reply. Can you provide example posts that are suitable for my case? What I found were all about comparing method applicable for the model fitting phase or for models from the same size of data sets.
– hlee
Aug 19, 2019 at 8:46
• Assuming comparison of curves means comparing the models (i.e. the Null would be all coefficients are statistically same), use chow.test in R. Only thing is that since you are taking sample from a dataset, there will be common data points. These data points will be repeated in the combined regression. This may give undue weight to these points. To handle this you can find out common set of data points in data1 and data2 and remove them from one of the dataset (say data1) and then use chow.test. Aug 19, 2019 at 10:14
• What about building a confidence interval for each estimation and check if the other estimation fits in it? Aug 19, 2019 at 11:24
• This actually isn't a duplicate--it's a totally bizarre situation in which the same model is being fit to different data, rather than different models to the same data (where you can just do anova). I've been thinking about how to answer it, but I think it was closed prematurely. Aug 20, 2019 at 1:16

Your case is that of comparing two models with same variables but different set of data points. You can use chow.test from library gap. However, as mentioned in my comment above, you need to remove common data points from one of the data frames. Following is the code for this (and also the result I obtained):

set.seed(3)
data1 = iris[sample(c(1:dim(iris)[1]), 30), ]
data2 = iris[sample(c(1:dim(iris)[1]), 50), ]

data1_sno=as.numeric(rownames(data1))
data2_sno=as.numeric(rownames(data2))

# next line tells you which of the data points in data2 are also in data1.
new_index=data2_sno %in% data1_sno
data2_new=data2[!new_index,]
gap::chow.test(y1=data1$$Petal.Length,x1=log(data1$$Petal.Width),
y2=data2_new$$Petal.Length,x2=log(data2_new$$Petal.Width))

F value      d.f.1      d.f.2    P value
0.8257495  2.0000000 64.0000000  0.4425175


So the null could not be rejected, i.e. it cannot be said that the curves are indeed different.

Why do a statistical test of this? Any test will have the usual problems of p values - that is, the result will depend partly on sample size.

When you have two data sets, you know that the same line doesn't fit both equally well (except perhaps for some constructed data sets and some very small data sets). So, graph both and look at them.

Are they different?

Then look at the predictions that each makes from the same IVs. Are they different?

And, by "different" I mean "different enough to matter to anyone".

• Thank you for your reply. Surely, they will be likely to differ from each other. I think that you replied like this because I provided little information on the term "different."
– hlee
Aug 22, 2019 at 5:26