1
$\begingroup$

I'm having trouble with a simple linear fit model. Here is my code

Clear["Global`*"]
x = {23.0, -5.0, 1.5, 1.5, 10.0, 0.0, 0.5}
num = Length[x]
absx = Abs[x]
r = (Ordering@Ordering@# + Reverse@Ordering@Ordering@Reverse@#)/2 &@absx // N
rstar = (r - 0.5)/num // N
p = (rstar + 1)/2
v = Sqrt[2]*InverseErf[2 p - 1]
(* create a table and sort data by r - the 3rd column *)

SortBy[Transpose[{x, absx, r, rstar, p, v}], #[[3]] &] // TableForm
dataset = SortBy[Transpose[{x, absx, r, rstar, p, v}], #[[3]] &]
(* collect the sorted absx variable *)
oabsx := dataset[[All, 2]]
(* collect the sorted v variable *)
ov = dataset[[All, 6]]
(* create the pairs of data to plot *)
xy = Transpose[{oabsx, ov}]
(* construct a linear model for the data *)
model = LinearModelFit[xy, x, x]
(* Plot the data and the linear model *)
Show[ListPlot[xy], Plot[model["BestFit"], {x, 0, Length[xy]}]]

This ListPlot shows up, but the fit does not; I get two errors in return

LinearModelFit::dpbss: The number of unique basis functions is 1 fewer than the number of basis functions specified in {23.,-5.,1.5,1.5,10.,0.,0.5}. Duplicated basis functions will be removed. >>

LinearModelFit::ivar: {23.,-5.,1.5,1.5,10.,0.,0.5} is not a valid variable. >> *)

Any suggestions ?

$\endgroup$
1
  • 1
    $\begingroup$ Hi ! You can head to the help centre and read more about proper code formatting practices. Aside from that try executing model = LinearModelFit[xy, x, x] and work from there. $\endgroup$
    – Sektor
    Jan 12, 2015 at 21:03

1 Answer 1

2
$\begingroup$

Your problem is that you are using x as the variable to represent the explanators in your model, but you have already defined x to be the list of numbers at the top.

Just change this line to be something other than x, like q:

model = LinearModelFit[xy, q, q] 

And don't forget to make the matching change in the Plot command:

Show[ListPlot[xy], Plot[model["BestFit"], {q, 0, Length[xy]}]]

enter image description 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.