0

I need to pull a vector from a normal distribution and normalize it to sum 0, because I want to simulate the power with the pwr.rasch() function from the pwrRasch package. Sounds easy enough.

I create the vector like this:

set.seed(123)
itempars <- rnorm(n = 10, mean = 0, sd = 1.8)  

To normalize the parameters to 0, I subtract the sum of my vector off of the last element of the vector like this:

itempars[10] <- itempars[10] - sum(itempars)

When I type sum(itempars) it should be 0, but it's -8.326673e-17. How is it possible? How can I get it to 0? I tried to round already, but it only increases the sum.

I don't want to choose every Itemparameter by hand. Thanks in advance!

EDIT:

Obviously the reasion is floating-point arithmetic. But it's hard to imagine that there is no way around.

The error massage of pwr.rasch() is as follows:

Error in simul.rasch(eval(parse(text = ppar[[1]])), ipar[[1]]) : 
Item pararameters are not normalized to sum-0

Sadly, the function has poor documentation. When I estimate groupwise itemparameters with eRm's RM() function, which has an extra argument for normalizing to sum 0, it gives me a similar difference like in my example.

Any trick'd come in handy as I don't want to create more than 50 normal distributed itemparameters per hand. Even worse. If I understood floating-point arithemtic corretly this problem can appear with the use of doubles in general. It'd be extremely limitating if I'd only be able to use integers as itemparameters.

j3ypi
  • 1,497
  • 16
  • 21
  • 4
    that's floating point precision – talat Oct 13 '17 at 13:58
  • 1
    https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal/9508558#9508558 . Can you give us more detail on why you need the sum to be *exactly* zero, i.e. what fails in `pwr.rasch()` when the sum is not exactly zero? – Ben Bolker Oct 13 '17 at 13:59
  • @docendodiscimus This answers the **Why**, but does not give a solution to my problem. Isn't there a hacky way to handle this? – j3ypi Oct 13 '17 at 17:12
  • @BenBolker See EDIT. – j3ypi Oct 13 '17 at 17:27
  • I think the question you originally asked has been answered - rather than make edits, I would suggest opening a new question about how to use `pwr.rasch()` with non-integers, but my guess is the only answer will be "if they have to sum to 0, you must use integers". It is too bad that `?pwr.rasch` doesn't provide any examples, but the help at `?itemtable` does have an example that uses `pwr.rasch`. See if that works, if it uses integers or not. – Gregor Thomas Oct 13 '17 at 18:16
  • @Gregor Well, I originally asked why it's wrong *and* how to fix it, so it's not completely off-topic, yet. I'm afraid you're right with the use of integers. I played around a little with doubles and it seems to me, that it's completely random, whether the sum is 0 or not. To find 50 doubles which sum exactly to 0, appears like some kind of utopia to me. – j3ypi Oct 13 '17 at 18:29
  • 1
    It's an XY problem. X was "why isn't this sum exactly 0 and how to I make it that" - with the answer being "inexactness is inherent in floating point arithmetic; you can't make it exactly 0." But your Y seems to be "how do I use `rasch::pwr.rasch` with non-integers", which may or may not have an answer you like. – Gregor Thomas Oct 13 '17 at 18:39
  • 2
    I don't have an objection to modification of the question to "how can I make this work". But please provide a **reproducible** example, e.g. `library(pwrRasch); pwr.rasch(c(5,5),ipar=list(scale(rnorm(5)),scale(rnorm(5))))`. Short answer: the code is (IMO) badly written. I don't see a straightforward way around it without hacking the code (we can show you how to do that) or contacting the maintainers. – Ben Bolker Oct 13 '17 at 19:20

1 Answers1

1

I downloaded the source code of the pwrRasch package and changed the if condition from

if (all(round(unlist(lapply(ipar, sum)), 3) != 0)) {

    stop("Item pararameters are not normalized to sum-0")

} 

to

if (all(round(unlist(lapply(ipar, sum)), 3) > 1e-5)) {

    stop("Item pararameters are not normalized to sum-0")

} 
j3ypi
  • 1,497
  • 16
  • 21