2
$\begingroup$

These two sets of data are as follows:

v1 = {0.04, 0.06, 0.04, 0.08, 0.08, 0.05, 0.05, 0.07, 0.07, 0.06}
v2 = {0.25, 0.4, 0.22, 0.54, 0.51, 0.34, 0.36, 0.46, 0.42, 0.4}
Correlation[v1, v2]

Take v1 data as the value range of the independent variable x in the regression linear equation, and v2 data as the value range of the dependent variable y in the regression linear equation. The data in v1 and v2 are corresponding according to the order and location

How to find the regression line equation of these two groups of data and draw the scatter diagram and regression line?

$\endgroup$

3 Answers 3

3
$\begingroup$

Something like this?

data = Thread@{v1, v2};
lm = LinearModelFit[data, x, x];
Show[
 ListPlot[data, PlotStyle -> Red],
 Plot[lm[x], {x, 0, 5}, PlotStyle -> {Dashed, Black}],
 AxesLabel -> {"v1", "v2"}]

lplm

$\endgroup$
1
$\begingroup$
v1 = {0.04, 0.06, 0.04, 0.08, 0.08, 0.05, 0.05, 0.07, 0.07, 0.06};

v2 = {0.25, 0.4, 0.22, 0.54, 0.51, 0.34, 0.36, 0.46, 0.42, 0.4};

data = Transpose[{v1, v2}];

Using Fit and Epilog

line = Line @ Transpose[{{0, 5}, Fit[data, {1, x}, x] /. {{x :> 0}, {x :> 5}}}]

ListPlot[data, Epilog -> {Dashed, line}, PlotStyle -> Red]

enter image description here

$\endgroup$
1
$\begingroup$

I post this to illustrate versatility of Mathematica/Wolfram Language:

(* Provided data *)

v1 = {0.04, 0.06, 0.04, 0.08, 0.08, 0.05, 0.05, 0.07, 0.07, 0.06};
v2 = {0.25, 0.4, 0.22, 0.54, 0.51, 0.34, 0.36, 0.46, 0.42, 0.4};

(* Linear algebra approach*)

a = {#, 1} & /@ v1
i = Inverse[Transpose[a] . a] . Transpose[a] . v2 . {x, 1};
Plot[i, {x, 0, 0.1}, Epilog -> {Red, Point[Thread[{v1, v2}]]}, 
 PlotStyle -> Dashed, PlotLabel -> i]

(* Using Fit *)

Fit[Thread[{v1, v2}], {1, x}, x]

(* Using LinearModelFit and confidence intervals *)

f[p_, pred_] := Module[{lm, cis},
  lm = LinearModelFit[Thread[{v1, v2}], x, x, ConfidenceLevel -> p];
  cis = Sort[Thread[{v1, #}]] & /@ 
    Transpose[
     lm[pred <> "PredictionConfidenceIntervalTableEntries"][[All, 4]]];
  Plot[lm[x], {x, 0.03, 0.08}, 
   Epilog -> {Line /@ cis, Red, Point[Thread[{v1, v2}]]}, 
   PlotRange -> {0, 0.6}, PlotStyle -> Dashed, PlotLabel -> lm[x]]]
Manipulate[f[p, i], {p, 0.5, 0.98}, {i, {"Mean", "Single"}}]

I am unable to upload image but output can be seen here

$\endgroup$

Your Answer

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

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