问题描述
这个问题建立在上一个问题的基础上,在这里为我很好地回答了.
This question builds upon a previous one which was nicely answered for me here.
R:具有rollapply的分组滚动窗口线性回归和ddply
您是否不知道将代码扩展到实际数据而不是示例数据时,代码无法正常工作吗?
Wouldn't you know that the code doesn't quite work when extended to the real data rather than the example data?
我有一个较大的数据集,具有以下特征.
I have a somewhat large dataset with the following characteristics.
str(T0_satData_reduced)
'data.frame': 45537 obs. of 5 variables:
$ date : POSIXct, format: "2014-11-17 08:47:35" "2014-11-17 08:47:36" "2014-11-17 08:47:37" ...
$ trial : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ vial : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
$ O2sat : num 95.1 95.1 95.1 95.1 95 95.1 95.1 95.2 95.1 95 ...
$ elapsed: num 20 20 20.1 20.1 20.1 ...
上一个问题涉及将O2sat
的滚动回归作为elapsed
的函数的愿望,但将回归按因子trial
和vial
分组.
The previous question dealt with the desire to apply a rolling regression of O2sat
as a function of elapsed
, but grouping the regressions by the factors trial
and vial
.
以下代码是从我上一个问题的答案中提取的(对完整数据集进行了简单修改,而不是对实践数据进行了修改)
The following code is drawn from the answer to my previous question (simply modified for the complete dataset rather than the practice one)
rolled <- function(df) {
rollapplyr(df, width = 600, function(m) {
coef(lm(formula = O2sat ~ elapsed, data = as.data.frame(m)))
}, by = 60, by.column = FALSE)
}
T0_slopes <- ddply(T0_satData_reduced, .(trial,vial), function(d) rolled(d))
但是,当我运行这段代码时,会收到一系列错误或警告(这里是前两个).
However, when I run this code I get a series of errors or warnings (first two here).
Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : - not meaningful for factors
由于我已经显示了elapsed
和O2sat
都是数字,所以我不确定该错误的来源,因此我不会对因素进行回归.但是,如果我这样在上面的rolled
函数中将它们都强制设为数字.
I'm not sure where this error comes from as I have shown both elapsed
and O2sat
are numeric, so I am not regressing on factors. However, if I force them both to be numeric within the rolled
function above like this.
...
coef(lm(formula = as.numeric(O2sat) ~ as.numeric(elapsed), data = as.data.frame(m)))
...
我不再收到错误,但是,我不知道为什么这可以解决错误.此外,由于截距项看起来过小,因此得出的回归看起来很可疑.
I no longer get the errors, however, I don't know why this would solve the error. Additionally, the resulting regressions appear suspect because the intercept terms seem inappropriately small.
关于我为什么会遇到这些错误以及为什么使用as.numeric
似乎可以消除这些错误的任何想法(如果可能仍然提供不合适的回归项)?
Any thoughts on why I am getting these errors and why using as.numeric
seems to eliminate the errors (if potentially still providing inappropriate regression terms)?
谢谢
推荐答案
rollapply
将矩阵传递给函数,因此仅传递数字列.使用我先前的回答中的rolled
和该问题中的设置:
rollapply
passes a matrix to the function so only pass the numeric columns. Using rolled
from my prior answer and the setup in that question:
do.call("rbind", by(dat[c("x", "y")], dat[c("w", "z")], rolled))
已添加
另一种实现方法是对行索引执行滚动应用,而不是对数据帧本身执行滚动.在此示例中,我们还添加了条件变量作为额外的输出列:
Another way to do it is to perform the rollapply over the row indexes instead of over the data frame itself. In this example we have also added the conditioning variables as extra output columns:
rolli <- function(ix) {
data.frame(coef = rollapplyr(ix, width = 6, function(ix) {
coef(lm(y ~ x, data = dat, subset = ix))[2]
}, by = 3), w = dat$w[ix][1], z = dat$z[ix][1])
}
do.call("rbind", by(1:nrow(dat), dat[c("w", "z")], rolli))
这篇关于R:rollapplyr和lm因数错误:rollapplyr会更改变量类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!