我如何使用rollapply与scale [英] how can I use rollapply with scale
问题描述
我有一个可以这样生成的数据:
I have a data which can be generated like this:
set.seed(1)
foo <- sample(1:10000,1000)
foo[c(1:100)] <- 1
在此之后,获得按比例计算的z值,我使用了:
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与scale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!