title: Doing curve fitting in R for a power series
body: <p>I have trying to estimate the annual of an investment scheme which applied dollar cost averaging, which the increment of value is listed as below (sorry that MathJax looks like greek to me, so I didn't use it):</p>
  
  <pre><code>x: regular contribution, which is 1500 in my case
  y: rate of return PLUS 100% (e.g 1.07 for a 7% return), which is the parameters that I want to estimate.
  
  Time 1: xy
  Time 2: (x + xy)*y
  Time 3: (x + (x + xy)*y)*y
  Time 4: (x + (x + (x + xy)*y)*y)*y
  Time 5: (x + (x + (x + (x + xy)*y)*y)*y)*y
  Time 6: (x + (x + (x + (x + (x + xy)*y)*y)*y)*y)*y
  </code></pre>
  
  <p>And the list goes on.</p>
  
  <p>After simplifying the equation, if I calculate it correctly, it should be a power series:</p>
  
  <pre><code>xy(1 + y + y^2 + y^3 + y^4 + y^5 +y^6)
  </code></pre>
  
  <p>I know that equation above can be used for the <code>nls</code> function in R, as suggested in <a href="http://www.walkingrandomly.com/?p=5254" rel="nofollow">http://www.walkingrandomly.com/?p=5254</a>. But the problem is, the investment scheme has been traded for more than 6 times, the number of trades is variable and I prefer not to fix the number of trades in the formula.</p>
  
  <p>I wonder if R can create a formula with variable length of the power series, something like a function?</p>
  
  <h3>update 01</h3>
  
  <p>Thanks for the comment @Roland, I have <code>dput</code> my dataframe as below:</p>
  
  <pre><code>structure(list(date = 1:62, value = c(1500, 3008.1048, 4279.09223337264, 
  5701.16001583254, 7545.25391699441, 8883.87795645887, 11192.7249445628, 
  13043.5267669473, 14396.3707754063, 16677.2027610312, 18474.8268536672, 
  20225.6882177597, 21889.6372952495, 24090.0451286292, 25305.8719822623, 
  26293.5164474925, 27470.5608573055, 26851.4637959011, 25610.4708389126, 
  29781.3033136099, 30244.449772352, 31757.1977515, 35216.3065708333, 
  38661.857424377, 40153.0021899712, 41453.1839013205, 39626.0241467687, 
  42464.6515262833, 44415.7606695956, 46456.2932413184, 49539.1291983018, 
  51223.0944673951, 53534.0828137635, 56511.2727118443, 60750.8112270199, 
  62420.4165280642, 64561.1738159384, 67269.7609015725, 69582.2433935286, 
  68461.4426685366, 72790.7668201147, 73029.1128824367, 77963.2040906503, 
  81782.8304828104, 84781.7088147301, 87010.8577769314, 85461.5060309602, 
  90165.7453255817, 91340.1347579196, 92918.1083054977, 96713.3387975151, 
  99841.2477244806, 101538.099862003, 104946.468993318, 103233.508326534, 
  106416.67466519, 109991.955526668, 110800.989092493, 112258.758666778, 
  118567.887527905, 120872.966926589, 127711.586247323), expected_value = c(1511.96121064336, 
  3035.97901230344, 4572.1495459301, 6120.56971911465, 7681.33721220313, 
  9254.55048445835, 10840.3087802712, 12438.7121354211, 14049.861383387, 
  15673.8581617081, 17310.8049183956, 18960.8049183956, 20623.9622501033, 
  22300.3818319294, 23990.1694189188, 25693.4316094218, 27410.2758518191, 
  29140.8104512998, 30885.1445766939, 32643.3882673588, 34415.6524401213, 
  36202.0488962746, 38002.6903286308, 39817.6903286308, 41647.1633935093, 
  43491.224933518, 45349.9912792063, 47223.5796887596, 49112.1083553966, 
  51015.6964148254, 52934.4639527589, 54868.5320124903, 56818.0226025291, 
  58783.0587042976, 60763.7642798896, 62760.2642798896, 64772.6846512559, 
  66801.1523452654, 68845.7953255225, 70906.7425760312, 72984.1241093319, 
  75078.0709747036, 77188.7152664305, 79316.190132135, 81460.6297811776, 
  83622.169493123, 85800.9456262742, 87997.0956262742, 90210.7580347771, 
  92442.0724981876, 94691.1797764704, 96958.2217520299, 99243.3414386608, 
  101546.68299057, 103868.391711469, 106208.614063744, 108567.497677691, 
  110945.191360831, 113341.845107297, 115757.610107297, 118192.63875665, 
  120647.084666402)), .Names = c("date", "value", "expected_value"
  ), row.names = c("63", "62", "61", "60", "59", "58", "57", "56", 
  "55", "54", "53", "52", "51", "50", "49", "48", "47", "46", "45", 
  "44", "43", "42", "41", "40", "39", "38", "37", "36", "35", "34", 
  "33", "32", "31", "30", "29", "28", "27", "26", "25", "24", "23", 
  "22", "21", "20", "19", "18", "17", "16", "15", "14", "13", "12", 
  "11", "10", "9", "8", "7", "6", "5", "4", "510", "410"), class = c("tbl_df", 
  "tbl", "data.frame"))
  </code></pre>