# Use FIT to calculate regression line from two sets of data

I figure out how to figure out the regression line using fit {I need to use FIT} for one set of number. However, I have to combine two sets and figure out the regression line.

 numberset1 = {1,2,3,4,5}
numberset2 = {6,5,6,7,8}


I am doing this to figure out the regression line for the first set.

 line = Fit[numberset1, {1, x}, x]


Then I graph both sets in a scatter plot with the regression line for set 1. However, I need to use both sets to figure out one regression line. I tried doing this

 data = {numberset1, numberset2}
Then

line = Fit[data, {1, x}, x]


but this does not work Ive also tried

  line = Fit[{numberset1,numberset2}, {1, x}, x]


did not work

Any ideas, I've already looked at the FIT line documentation in mathematica. None of the examples work.

• Why not line = Fit[Join[numberset1,numberset2], {1, x}, x] ? Also, please clarify what are data2 and data1 in your code. – K.J. Nov 22 '17 at 9:15
• Or maybe you need Fit[Transpose[{numberset1, numberset2}], {1, x}, x] ? – K.J. Nov 22 '17 at 9:21

The way the question is phrased you have two lists that need to be joined and then apply a linear fit to the joined list.

To join them use:

data = Join[numberset1, numberset2]
(* {1, 2, 3, 4, 5, 6, 5, 6, 7, 8} *)


Now apply Fit to the data

line = Fit[data, {1, x}, x]
(* 0.8 + 0.709091 x *)


The answer is approximatly to 0.71*x + 0.8.

Note that data in this form input to Fit assumes that the x input equals {1,2,3,4,5,6,7,8,9,10} and the y output is the data.

Another function that can be used for fitting is LinearmodelFit

lm = LinearModelFit[data, x, x]


An advantage of this approach is that you have access to all sorts of valuable statistical information on the data and fit (see documentation).

To compare the data to the fit the linear model lm can be used as a function.

Show[
ListPlot[data, PlotStyle -> Black],
Plot[lm[x], {x, 1, 10}, PlotStyle -> Red]
]