当我尝试在日期列上使用 apply() 条件来返回一组系数时,我收到一条错误消息。
我有一个数据集(为简单起见,此处进行了修改,但可重现):
ADataset <- 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
分析从确定开始和结束日期开始:
############## Establish dates for analysis
#4.Set date for center of duration
StartDate <- "2007-11-18"
as.numeric(as.Date(StartDate)); StartDate
EndDate <- as.Date(tail(Adataset$Epoch,1)); EndDate
然后我建立分析的持续时间:
#5.Quantify duration of time window
STDuration <- 1
LTDuration <- 3
然后我编写函数在两个持续时间上回归并返回斜率:
# 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 & Long Term datasets
#8. Run regression over dataset
my_STS_Function <- function (StartDate) {
STAhead <- as.Date(StartDate) + STDuration; STAhead
STBehind <- as.Date(StartDate) - STDuration; STBehind
STDataset <- subset(Adataset, as.Date(Epoch) >= STBehind & as.Date(Epoch)<STAhead)
STResults <- rlm( Distance ~ Epoch, data=STDataset); STResults
STSummary <- summary( STResults ); STSummary
# Return coefficient (Slope of regression)
STNum <- STResults$coefficients[2];STNum
}
my_LTS_Function <- function (StartDate) {
LTAhead <- as.Date(StartDate) + LTDuration; LTAhead
LTBehind <- as.Date(StartDate) - LTDuration; LTBehind
LTDataset <- subset(Adataset, as.Date(Epoch) >= LTBehind & as.Date(Epoch)<LTAhead)
LTResults <- rlm( Distance ~ Epoch, data=LTDataset); LTResults
LTSummary <- summary( LTResults ); LTSummary
# Return coefficient (Slope of regression)
LTNum <- LTResults$coefficients[2];LTNum
然后我测试该函数以确保它适用于单个日期:
myTestResult <- my_STS_Function("2007-11-18")
它有效,因此我继续在数据集中的日期范围内应用该函数:
mySTSResult <- apply(Adataset, 1, my_STS_Function, seq(StartDate : EndDate))
...其中我想要的结果是 mySTSResult (斜率)的列表或数组或向量(以及随后的 myLTSResults 的单独列表/数组/向量,因此我可以在持续时间内创建 STSlope:LTSlope 比率),某事像(mySTSResults 捏造)...
> 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
只有我得到这个错误:
Error in FUN(newX[, i], ...) : unused argument(s) (1:1185)
这告诉我什么以及如何纠正它?我做了一些查找,找不到更正。
希望我已经充分解释了这一点。如果您需要更多详细信息,请告诉我。