I wanted to follow up on a question asked before about the all.moments function from the moments package: all.moments function weird outcome
The distribution – specifically the asymptotic variance – of the output of all.moments does not seem to align with standard theory for the 3rd and 4th moments, or I am doing something incorrectly.
Consider the following code:
reps <- 100000 # number of repetitions
n <- 1000000 # sample size
asy.cmtx <- matrix(0,nrow=reps,ncol=5) # initialize our matrix of conventional moments
for(i in 1:reps){
vec <- rnorm(n,mean=0,sd=1) # create vector of 1M standard normals
asy.cmtx[i,] <- all.moments(vec,order.max=5)[2:6] # get sample moments
}
mean(asy.cmtx[,3]) # this should be 0
# [1] 3.972406e-06
mean(asy.cmtx[,4]) # this should be 3
# [1] 2.999996
var(sqrt(n)*asy.cmtx[,3]/(asy.cmtx[,2]^(3/2))) # this should be 6
# [1] 14.41766
var(sqrt(n)*(asy.cmtx[,4]-3)/(asy.cmtx[,2]^(2))) # this should be 24
# [1] 96.25745
The asymptotic variance seems larger than expected.
I went back and simply used the sample moment formulae that we all know and got the correct outcome:
reps <- 100000 # number of repetitions
n <- 1000000 # sample size
asy.34.2 <- matrix(0,nrow=reps,ncol=2) # initialize our matrix of conventional moments
for(i in 1:reps){
y <- rnorm(n,mean=0,sd=1) # create vector of 1M standard normals
y.bar <- mean(y)
m2 <- ((1/n)*sum((y-y.bar)^2))
asy.34.2[i,1] <- ((1/n)*sum((y-y.bar)^3))/(m2^(3/2))
asy.34.2[i,2] <- ((1/n)*sum((y-y.bar)^4))/(m2^2)
}
mean(asy.34.2[,1])
# [1] 7.512593e-06
mean(asy.34.2[,2])
# [1] 2.999985
var(sqrt(1000000)*asy.34.2[,1]) # this should be 6
# [1] 5.990771
var(sqrt(1000000)*(asy.34.2[,2]-3)) # this should be 24
# [1] 24.23367
So the actual formulae have the variance that we would expect, but all.moments does not, even though it has the proper expectation. This should not be an issue due to sample size or number of iterations because both are large in this case (1M and 100k respectively in both cases). Has anyone had this issue before?
Also, has anyone had issues with R simply dropping your output? When I attempted to manipulate the output above, somehow the output “vanished”, with dim = NULL. However if the first thing I do is write.csv() the data, I can then read it back in and manipulate without it vanishing on me.
There are two errors: (1) as flodel points out you need central moments not raw moments, this won’t make much difference for point estimates of the moments but it does make a difference for estimates of their variance; (2) there is a bug in the computation of the kurtosis when using all.moments. When we apply these corrections we have:
…
…