Calculate cumulative standard deviation [duplicate] - r

This question already has answers here:
Efficient calculation of matrix cumulative standard deviation in r
(2 answers)
Closed 9 years ago.
I'm trying to calculate the standard deviation of values in a time series, but I'd like to do it incrementally by advancing one day from the initial date value each time. I know there is a way to do this in R (probably using ddply?) that doesn't involve a nasty for-loop. Thanks for any help!
d<-seq(from=as.Date("2013-01-01"), to=as.Date("2013-02-01"), by="day")
v <-rnorm(32, 10, 5)
test.df<-data.frame(the_date=d, value=v)
Here's the way I'm doing it now.
result <- c()
for(i in 2:nrow(test.df)){ result[i-1] <- sd(test.df[1:i,]$value)}

Use TTR::runSD with cumulative=TRUE.
library(TTR)
x <- xts(test.df[,2],test.df[,1])
runSD(x, n=1, cumulative=TRUE)

Related

Reverse Z Scores [duplicate]

This question already has an answer here:
Inverse of Z-Normalize (z-score) Function on Matlab
(1 answer)
Closed 2 years ago.
If you have a data vector you are given that contains only z-scored variables is it possible to reverse the z-score values to get the original measure without using the original measure?
In R Program:
a = runif(100)
az = (a - mean(a))/sd(a)
Can you get back 'a' using JUST 'az'?
No, it is not possible. The reverse transformation requires knowledge of the summary statistics mean(a) and sd(a), which cannot be recovered from the standardized scores.

Average of each column in a matrix in R [duplicate]

This question already has answers here:
calculate the mean for each column of a matrix in R
(10 answers)
Closed 4 years ago.
I'm new to R and still learning. I have a matrix with 100 columns and I need to calculate the average of each column and store all those values for further calculations. Each column has 5 numbers and after this step i'm supposed to have 100 new values. Also, please let me know if the replicate() function is a viable way to do this in just one line.
colMeans(DF)
Is a highly optimized function for exactly this purpose.

How to calculate cumulative mean in R? [duplicate]

This question already has answers here:
Calculate cumulative average (mean)
(7 answers)
Closed 5 years ago.
(I am sorry if the term is not correct).
In R, I have a numeric vector x. I want to create new vector y where:
y[i] = mean (x[1:i)
It is easy to write a function to calculate y, but is there a built-in function in R which do the task?
Thank you very much
Try this
y <- cumsum(x) / seq_along(x)
Reference
https://stat.ethz.ch/pipermail/r-help/2008-May/162729.html

Set sum random sample [duplicate]

This question already has answers here:
Generate N random integers that sum to M in R
(3 answers)
Closed 7 years ago.
So this seems like a really simple question to me, but I can't seem to figure it out. I'm using R and I'm trying to generate a random sample, where the generated sample all sums to a set number.
Here's an approach to consider. Generate some random numbers:
n = 10
x <- runif(n) # or rnorm, rpois, whatever you want to use
And then scale() them to get the sum you want.
tot = 100 # this is the sum you want
x <- scale(x, center=FALSE, scale=sum(x)/tot)
all.equal(sum(x), tot) #TRUE

For-Loop indexing issue in R [duplicate]

This question already has answers here:
calculating mean for every n values from a vector
(3 answers)
Closed 1 year ago.
I have 28 groups of 48 rows in an R dataframe. I'm trying to take the standard deviation of each group. I used the following statement in R Studio:
stddev <- vector();
for (i in 1:28) { stddev[i] <- sd(in.subj[((i * 48) -47):(i * 48), 5]); }
When I check the values of stddev[] afterward, stddev[1] = NA. Likewise, when I check the standard deviations of individual groups, like sd(in.subj[49:96,5]) I get different values than the for loop printed out.
What would be the cause of these issues?
Thanks!
you can try :
tapply(in.subj[,5], gl(28,48), sd)
if there is some NAs in your data :
tapply(in.subj[,5], gl(28,48), sd, na.rm=T)

Resources