0

I used two formulas for calculating the variance which were almost the same but the derivation differs.

1) xVarCom <- (xSquareSum - (n*xMeanSquare)) / (n-1) [Note: Mean square = sum square/n and squaresum is 1square+2square.....nsquare]

2) varLoop <- (varLoop + (x[i]-(xMean)^2)/(n-1)

I have given the limits as 1:n. Both gives the same variance, but when I give higher values of mean like more than 100 thousands the first one is getting break down. Also the first expression elapsed time is faster than the second one. why is this happening?

nicola
  • 24,005
  • 3
  • 35
  • 56
  • You should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can run and compare. When it comes to performance timing, be sure to include your OS and R version info (from `sessionInfo()`) – MrFlick Sep 14 '16 at 15:37
  • 1
    Why reinvent the wheel? The `var` function already exists in R and calculates the variance. – nicola Sep 14 '16 at 15:42
  • Plus `var` does the heavy lifting in C, so it will be fast. – alistaire Sep 14 '16 at 15:44
  • *"why is this happening?"* Cannot answer based on the paucity of information you've provided us. As @MrFlick requested, please provide a reproducible example, including data and/or code to generate the data, as well as your session info. (Please do not put any of this in a comment; just edit the question with an "Update:".) Additional suggested reading: [StackOverflow Help](http://stackoverflow.com/help/mcve). – r2evans Sep 14 '16 at 16:39

0 Answers0