1
$\begingroup$

I am attempting to perform a weighted fit of some data. The Fit function produces the correct quadratic fit, which I am fine with, but it does not appear to support weighted fitting. Therefore, I had to use the LinearModelFit and NonlinearModelFit functions. The linear model performed as expected, but the nonlinear model outputted a very large array of nonsensical results (eg a=0,b=0,c=0.003). Attempting to fit a linear model using the function also failed, with the same huge array of results.

What am I doing wrong with the functions? I have tried to follow the examples as closely as I could, but still cannot get it to work.

alist = {{75, 0.`}, {76, 3/5}, {77, 9/11}, {78, 6/7}, {79, 8/13}, {80,
     2/3}, {81, 5/7}, {82, 7/9}, {83, 6/7}, {84, 5/9}, {85, 1/4}, {86,
     4/7}, {87, 2/3}, {88, 1}, {89, 1}, {90, 1/2}, {91, 3/7}, {92, 
    1}, {93, 5/6}, {94, 2/3}, {95, 2/5}, {96, 3/5}, {97, 5/7}, {98, 4/
    7}, {99, 6/7}, {100, 3/4}, {101, 5/7}, {102, 9/10}, {103, 7/
    10}, {104, 5/6}, {105, 5/8}, {106, 3/7}, {107, 11/15}, {108, 13/
    18}, {109, 9/11}, {110, 11/14}, {111, 8/9}, {112, 7/8}, {113, 2/
    3}, {114, 9/10}, {115, 10/13}, {116, 9/11}, {117, 8/11}, {118, 5/
    6}, {119, 11/13}, {120, 5/7}, {121, 14/19}, {122, 7/11}, {123, 13/
    17}, {124, 17/21}, {125, 11/13}, {126, 15/17}, {127, 7/11}, {128, 
    17/19}, {129, 15/17}, {130, 15/19}, {131, 9/11}, {132, 7/8}, {133,
     26/27}, {134, 23/24}, {135, 24/25}, {136, 29/31}, {137, 29/
    34}, {138, 17/20}, {139, 30/37}, {140, 31/36}, {141, 16/17}, {142,
     22/23}, {143, 33/35}, {144, 1}, {145, 7/8}, {146, 1}, {147, 
    1}, {148, 1}, {149, 7/8}, {150, 1}};
wlist = {0, 5, 11, 7, 13, 6, 7, 9, 7, 9, 4, 7, 3, 2, 1, 4, 7, 3, 6, 
   12, 5, 5, 7, 7, 7, 8, 14, 20, 10, 18, 16, 7, 15, 18, 11, 14, 9, 8, 
   9, 10, 13, 11, 11, 6, 13, 7, 19, 11, 17, 21, 26, 17, 11, 19, 17, 
   19, 22, 16, 27, 24, 25, 31, 34, 40, 37, 36, 51, 23, 35, 22, 24, 31,
    27, 26, 8, 54};
linmod = LinearModelFit[alist, x, x, Weights -> wlist]
quadmod = 
 NonlinearModelFit[alist, ax^2 + bx + c, {a, b, c}, x, 
  Weights -> wlist]
linmod2 = 
 NonlinearModelFit[alist, ax + b, {a, b}, x, Weights -> wlist]
$\endgroup$
1
  • 2
    $\begingroup$ Seems like a typographical problem. Beware of the difference:ax^2 + bx + c and a x^2 + b x + c: I get the expected result when typing in the quadratic function with spaces between a,b and x $\endgroup$
    – mgamer
    Feb 11, 2015 at 15:21

1 Answer 1

2
$\begingroup$
linmod  = LinearModelFit[alist, x, x, Weights -> wlist];
quadmod = NonlinearModelFit[alist, a x^2 + b x + c, {a, b, c}, x,  Weights -> wlist];
linmod2 = NonlinearModelFit[alist, a x + b, {a, b}, x, Weights -> wlist];

Normal /@ {linmod, linmod2, quadmod}
(*
{0.265819 + 0.00461482 x, 
 0.265819 + 0.00461482 x, 
 1.0223 - 0.00877669 x + 0.0000572598 x^2}

*)
$\endgroup$
3
  • 1
    $\begingroup$ Yes, that´s what I meant in my comment ;-) $\endgroup$
    – mgamer
    Feb 11, 2015 at 16:25
  • $\begingroup$ @mgamer True. I haven't seen your comment. You probably posted it while I was writing down this answer. Sorry for the "theft" $\endgroup$ Feb 11, 2015 at 16:28
  • $\begingroup$ No problem. I know this situation also "the other way round" :-) $\endgroup$
    – mgamer
    Feb 11, 2015 at 21:21

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.