问题描述
我有一个可以这样生成的数据:
I have a data which can be generated like this:
set.seed(1)
foo <- sample(1:10000,1000)
foo[c(1:100)] <- 1
在此之后得到按比例计算的 zvalues,我使用了:
After this to get the zvalues, which are calculated by scale, I used:
boo<-rollapply(foo,50,scale)
但是 boo 的所有值似乎都是 NAN.
But all the values of boo seems to be NAN.
背景信息:
z-score = scale = (x - mean)/ std deviation
我的第一个问题是为什么所有值都得到 NAN?对于前 100 个,我知道 std dev 是 o .所以,我应该只为前几行获取 Nan,但我为所有行获取 NAN.我不明白我错在哪里.
My first question is why do I get NAN for all the values? For the first 100, I understand that std dev is o . So, I should get Nan only for the first few rows, but I get NAN for all the rows . I do not understand where I am wrong.
第二个问题是我的实际问题.
Second question is my actual problem.
我想获取一个包含 50 个元素的窗口并仅获取窗口第 25 个或中间元素的 z 分数.然后我需要对所有 1000 个数据点进行滚动应用.
I want to take a window of 50 elements and get the z-score only for the 25th or mid element of the window.Then I need to rollapply for all the 1000 datapoints.
因此,对于其各自的 50 个窗口大小,输出将是从 25 到 975 的元素的 z 分数.如何使用 rollapply 和 scale 获得此结果?
So , the output will be z-score of elements from 25 to 975 for its respective 50 window size.How can i get this result using rollapply and scale?
推荐答案
1) rollapply
期望 FUN
返回标量或向量,不是列矩阵.返回一个向量将消除不需要的 NaN 值:
1) rollapply
expects FUN
to return a scalar or a vector, not a column matrix. Returning a vector will eliminate the unwanted NaN values:
rollapply(foo , 50, function(x) c(scale(x)))
结果将是一个 951x50 的矩阵.
The result will be a 951x50 matrix.
2) 对于第二个问题,试试这个:
2) For the second question try this:
rollapply(foo, 50, function(x) (x[25] - mean(x)) / sd(x))
或者这个:
rollapply(foo, 50, function(x) scale(x)[25])
或者这个:
rollapply(foo, 50, function(x) c(scale(x)))[, 25]
这篇关于我如何使用带有比例的 rollapply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!