Search code examples
rparametersmeanstandard-deviationstan

Find out how many standard deviations a parameters mean is from 0 R


I have a stanfit object, that I have made into a data frame. This data frame consists of 15 columns and thousands of rows. Each column is a parameter (for example Beta1, Beta2, Beta3, where each Beta is some kind of landcover). First step was to extract only beta parameters from the stanfit object.

beta <- as.data.frame(data)
beta <- beta[, grep("beta", names(beta))]

And my data looks something like this:

beta <- data.frame(Beta1 = c(-7.595932, -6.451768, -4.682111, -8.781488, -4.251690), 
                   Beta2 = c(0.8324450, 0.9451657, 0.8773759, 0.6044753, 0.6553995),
                   Beta3= c(22.747480, 15.477470, 18.745407, 9.622865, 21.137619), 
                   Beta4 = c(-11.684762, -13.474299, -9.783277, -7.747501, -12.352081))

As I mentioned, in my real data I have thousands of rows. I want to find out how many standard deviations each betas(beta1,2,3,4) mean is from 0. I tried to use the summary function, but here I only get quantiles, mean, and median. I want to know how close or far away each parameter is from zero, that's why I want to know how many standard deviations the parameters mean is from 0.

Have anyone experienced the same problem?


Solution

  • Probably faster using matrixStats package.

    library(matrixStats)
    beta <- as.matrix(beta)
    colMeans2(beta) / colSds(beta)
    # [1] -3.318340  5.345975  3.369771 -4.865303