2
$\begingroup$

I dont know if I am confused or if I am doing this incorrectly.

I have two data sets

 a={1,2,3,4,5}
 b={6,7,8,9,10}

Mathematica will translate the sets as

 a = {1,1},{2,2},{3,3},{4,4},{5,5}
 b = {1,6},{2,7},{3,8},{4,9},{5,10}

I want to do a scatter plot and find the regression line. I know how to do a scatter plot but I am not sure how to do a regression line for both data sets.

I looked around and it was suggested to me that I can use this

 regline = Fit[Join{a,b},{1,x},x]

 Show[ListPlot[{a,b}], Plot[{regline}, {x, 0, 10}]]

This works but the "Join" makes the data sets A and B like this

 {1,2,3,4,5,6,7,8,9,10}

and translates to this.

{1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7},{8,8},{9,9},{10,10}.

Wouldn't the regression line need to be figure out on the data sets

 {1,1},{2,2},{3,3},{4,4},{5,5}
 {1,6},{2,7},{3,8},{4,9},{5,10}

and not

 {1,1},{2,2},{3,3},{4,4},{5,5},{6,6},{7,7},{8,8},{9,9},{10,10}.

The regression line would be different for both.

$\endgroup$

1 Answer 1

1
$\begingroup$

Maybe this is what you are looking for:

a = {1, 2, 3, 4, 5};
b = {6, 7, 8, 9, 10};
data = Join[Transpose[{Range[Length[a]], a}], Transpose[{Range[Length[b]], b}]]
regline = Fit[data, {1, x}, x]
Show[ListPlot[data], Plot[{regline}, {x, 0, 10}]]

{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {1, 6}, {2, 7}, {3, 8}, {4, 9}, {5, 10}}

{2.5 + 1. x}

enter image description here

$\endgroup$
4
  • $\begingroup$ I see why you did data = Join[Transpose[{a, a}], Transpose[{a, b}]] but how would you did it if data points were a={11,23,55,33,55} and b = {4,66,56,56,55} @HenrikSchumacher $\endgroup$ Nov 28, 2017 at 0:40
  • $\begingroup$ @user2896245 Better now? I use Range to produce {1,2,3,4,5}... $\endgroup$ Nov 28, 2017 at 0:43
  • $\begingroup$ Yes, Thank you! You are the best! $\endgroup$ Nov 28, 2017 at 0:45
  • $\begingroup$ @user2896245 You're welcome! $\endgroup$ Nov 28, 2017 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.