Note that there are some explanatory texts on larger screens.

plurals
  1. POunused arguments error using apply() in R
    text
    copied!<p>I get an error message when I attempt to use apply() conditional on a column of dates to return a set of coefficients.</p> <p>I have a dataset (herein modified for simplicity, but reproducible):</p> <pre><code>ADataset &lt;- data.table(Epoch = c("2007-11-15", "2007-11-16", "2007-11-17", "2007-11-18", "2007-11-19", "2007-11-20", "2007-11-21"), Distance = c("92336.22", "92336.23", "92336.22", "92336.20", "92336.19", "92336.21", "92336.18)) ADataset Epoch Distance 1: 2007-11-15 92336.22 2: 2007-11-16 92336.23 3: 2007-11-17 92336.22 4: 2007-11-18 92336.20 5: 2007-11-19 92336.19 6: 2007-11-20 92336.21 7: 2007-11-21 92336.18 </code></pre> <p>The analysis begins with establishing start and end dates:</p> <pre><code>############## Establish dates for analysis #4.Set date for center of duration StartDate &lt;- "2007-11-18" as.numeric(as.Date(StartDate)); StartDate EndDate &lt;- as.Date(tail(Adataset$Epoch,1)); EndDate </code></pre> <p>Then I establish time durations for analysis:</p> <pre><code>#5.Quantify duration of time window STDuration &lt;- 1 LTDuration &lt;- 3 </code></pre> <p>Then I write functions to regress over both durations and return the slopes:</p> <pre><code># Write STS and LTS functions, each with following steps #6.Define time window- from StartDate less ShortTermDuration to StartDate plus ShortTermDuration #7.Define Short Term &amp; Long Term datasets #8. Run regression over dataset my_STS_Function &lt;- function (StartDate) { STAhead &lt;- as.Date(StartDate) + STDuration; STAhead STBehind &lt;- as.Date(StartDate) - STDuration; STBehind STDataset &lt;- subset(Adataset, as.Date(Epoch) &gt;= STBehind &amp; as.Date(Epoch)&lt;STAhead) STResults &lt;- rlm( Distance ~ Epoch, data=STDataset); STResults STSummary &lt;- summary( STResults ); STSummary # Return coefficient (Slope of regression) STNum &lt;- STResults$coefficients[2];STNum } my_LTS_Function &lt;- function (StartDate) { LTAhead &lt;- as.Date(StartDate) + LTDuration; LTAhead LTBehind &lt;- as.Date(StartDate) - LTDuration; LTBehind LTDataset &lt;- subset(Adataset, as.Date(Epoch) &gt;= LTBehind &amp; as.Date(Epoch)&lt;LTAhead) LTResults &lt;- rlm( Distance ~ Epoch, data=LTDataset); LTResults LTSummary &lt;- summary( LTResults ); LTSummary # Return coefficient (Slope of regression) LTNum &lt;- LTResults$coefficients[2];LTNum </code></pre> <p>Then I test the function to make sure it works for a single date:</p> <pre><code>myTestResult &lt;- my_STS_Function("2007-11-18") </code></pre> <p>It works, so I move on to apply the function over the range of dates in the dataset:</p> <pre><code>mySTSResult &lt;- apply(Adataset, 1, my_STS_Function, seq(StartDate : EndDate)) </code></pre> <p>...in which my desired result is a list or array or vector of mySTSResult (slopes) (and, subsequently, a separate list/array/vector of myLTSResults so then I can create a STSlope:LTSlope ratio over the duration), something like (mySTSResults fabricated)...</p> <pre><code>&gt; Adataset Epoch Distance mySTSResults 1: 2007-11-15 92336.22 3 2: 2007-11-16 92336.23 4 3: 2007-11-17 92336.22 5 4: 2007-11-18 92336.20 6 5: 2007-11-19 92336.19 7 6: 2007-11-20 92336.21 8 7: 2007-11-21 92336.18 9 </code></pre> <p>Only I get this error:</p> <pre><code>Error in FUN(newX[, i], ...) : unused argument(s) (1:1185) </code></pre> <p>What is this telling me and how to do correct it? I've done some looking and cannot find the correction.</p> <p>Hopefully I've explained this sufficiently. Please let me know if you need further details.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload