How do I make LinearModelFit flexible to number of parameters using FromLetterNumber?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I want to run LinearModelFit where the number of parameters is not fixed. For example, the following



i = 2;
data = RandomReal[20,10,3+i];
lm1 = LinearModelFit[data,FromLetterNumber[Range[2+i]],FromLetterNumber[Range[2+i]];


doesn't work, whereas



lm1 = LinearModelFit[data,a,b,c,d,a,b,c,d]


works as per described, despite FromLetterNumber[Range[2+i]] == a,b,c,d



Can this be made to work using some kind of Hold or do I need to resort to a design matrix, response vector format?







share|improve this question























    up vote
    4
    down vote

    favorite












    I want to run LinearModelFit where the number of parameters is not fixed. For example, the following



    i = 2;
    data = RandomReal[20,10,3+i];
    lm1 = LinearModelFit[data,FromLetterNumber[Range[2+i]],FromLetterNumber[Range[2+i]];


    doesn't work, whereas



    lm1 = LinearModelFit[data,a,b,c,d,a,b,c,d]


    works as per described, despite FromLetterNumber[Range[2+i]] == a,b,c,d



    Can this be made to work using some kind of Hold or do I need to resort to a design matrix, response vector format?







    share|improve this question





















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I want to run LinearModelFit where the number of parameters is not fixed. For example, the following



      i = 2;
      data = RandomReal[20,10,3+i];
      lm1 = LinearModelFit[data,FromLetterNumber[Range[2+i]],FromLetterNumber[Range[2+i]];


      doesn't work, whereas



      lm1 = LinearModelFit[data,a,b,c,d,a,b,c,d]


      works as per described, despite FromLetterNumber[Range[2+i]] == a,b,c,d



      Can this be made to work using some kind of Hold or do I need to resort to a design matrix, response vector format?







      share|improve this question











      I want to run LinearModelFit where the number of parameters is not fixed. For example, the following



      i = 2;
      data = RandomReal[20,10,3+i];
      lm1 = LinearModelFit[data,FromLetterNumber[Range[2+i]],FromLetterNumber[Range[2+i]];


      doesn't work, whereas



      lm1 = LinearModelFit[data,a,b,c,d,a,b,c,d]


      works as per described, despite FromLetterNumber[Range[2+i]] == a,b,c,d



      Can this be made to work using some kind of Hold or do I need to resort to a design matrix, response vector format?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Aug 6 at 7:54









      Thadeu Freitas Filho

      866




      866




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          FromLetterNumber returns a String:



          Head @ FromLetterNumber @ 3



          String




          You can wrap the strings returned by FromLetterNumber with Symbol or ToExpression to get Symbols:



          lm1 = LinearModelFit[data, Symbol /@ FromLetterNumber[Range[2+i]], 
          Symbol /@ FromLetterNumber[Range[2+i]]];
          Normal @ lm1



          9.60642 + 0.175108 a - 0.723711 b + 0.850788 c - 0.198777 d




          Alternatively,



          lm2 = LinearModelFit[data, Array[a, 2 + i], Array[a, 2 + i] ] ;
          Normal @ lm2



          9.60642 + 0.175108 a[1] - 0.723711 a[2] + 0.850788 a[3] - 0.198777 a[4]







          share|improve this answer



















          • 2




            The Array version should be the canonical choice, IMHO. +1
            – Marius Ladegård Meyer
            Aug 6 at 9:34










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f179557%2fhow-do-i-make-linearmodelfit-flexible-to-number-of-parameters-using-fromletternu%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          FromLetterNumber returns a String:



          Head @ FromLetterNumber @ 3



          String




          You can wrap the strings returned by FromLetterNumber with Symbol or ToExpression to get Symbols:



          lm1 = LinearModelFit[data, Symbol /@ FromLetterNumber[Range[2+i]], 
          Symbol /@ FromLetterNumber[Range[2+i]]];
          Normal @ lm1



          9.60642 + 0.175108 a - 0.723711 b + 0.850788 c - 0.198777 d




          Alternatively,



          lm2 = LinearModelFit[data, Array[a, 2 + i], Array[a, 2 + i] ] ;
          Normal @ lm2



          9.60642 + 0.175108 a[1] - 0.723711 a[2] + 0.850788 a[3] - 0.198777 a[4]







          share|improve this answer



















          • 2




            The Array version should be the canonical choice, IMHO. +1
            – Marius Ladegård Meyer
            Aug 6 at 9:34














          up vote
          5
          down vote



          accepted










          FromLetterNumber returns a String:



          Head @ FromLetterNumber @ 3



          String




          You can wrap the strings returned by FromLetterNumber with Symbol or ToExpression to get Symbols:



          lm1 = LinearModelFit[data, Symbol /@ FromLetterNumber[Range[2+i]], 
          Symbol /@ FromLetterNumber[Range[2+i]]];
          Normal @ lm1



          9.60642 + 0.175108 a - 0.723711 b + 0.850788 c - 0.198777 d




          Alternatively,



          lm2 = LinearModelFit[data, Array[a, 2 + i], Array[a, 2 + i] ] ;
          Normal @ lm2



          9.60642 + 0.175108 a[1] - 0.723711 a[2] + 0.850788 a[3] - 0.198777 a[4]







          share|improve this answer



















          • 2




            The Array version should be the canonical choice, IMHO. +1
            – Marius Ladegård Meyer
            Aug 6 at 9:34












          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          FromLetterNumber returns a String:



          Head @ FromLetterNumber @ 3



          String




          You can wrap the strings returned by FromLetterNumber with Symbol or ToExpression to get Symbols:



          lm1 = LinearModelFit[data, Symbol /@ FromLetterNumber[Range[2+i]], 
          Symbol /@ FromLetterNumber[Range[2+i]]];
          Normal @ lm1



          9.60642 + 0.175108 a - 0.723711 b + 0.850788 c - 0.198777 d




          Alternatively,



          lm2 = LinearModelFit[data, Array[a, 2 + i], Array[a, 2 + i] ] ;
          Normal @ lm2



          9.60642 + 0.175108 a[1] - 0.723711 a[2] + 0.850788 a[3] - 0.198777 a[4]







          share|improve this answer















          FromLetterNumber returns a String:



          Head @ FromLetterNumber @ 3



          String




          You can wrap the strings returned by FromLetterNumber with Symbol or ToExpression to get Symbols:



          lm1 = LinearModelFit[data, Symbol /@ FromLetterNumber[Range[2+i]], 
          Symbol /@ FromLetterNumber[Range[2+i]]];
          Normal @ lm1



          9.60642 + 0.175108 a - 0.723711 b + 0.850788 c - 0.198777 d




          Alternatively,



          lm2 = LinearModelFit[data, Array[a, 2 + i], Array[a, 2 + i] ] ;
          Normal @ lm2



          9.60642 + 0.175108 a[1] - 0.723711 a[2] + 0.850788 a[3] - 0.198777 a[4]








          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Aug 7 at 0:00


























          answered Aug 6 at 8:06









          kglr

          154k8179374




          154k8179374







          • 2




            The Array version should be the canonical choice, IMHO. +1
            – Marius Ladegård Meyer
            Aug 6 at 9:34












          • 2




            The Array version should be the canonical choice, IMHO. +1
            – Marius Ladegård Meyer
            Aug 6 at 9:34







          2




          2




          The Array version should be the canonical choice, IMHO. +1
          – Marius Ladegård Meyer
          Aug 6 at 9:34




          The Array version should be the canonical choice, IMHO. +1
          – Marius Ladegård Meyer
          Aug 6 at 9:34












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f179557%2fhow-do-i-make-linearmodelfit-flexible-to-number-of-parameters-using-fromletternu%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What is the equation of a 3D cone with generalised tilt?

          Relationship between determinant of matrix and determinant of adjoint?

          Color the edges and diagonals of a regular polygon