Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad starting values when nrep>1 #15

Open
adamSales opened this issue Apr 23, 2018 · 1 comment
Open

bad starting values when nrep>1 #15

adamSales opened this issue Apr 23, 2018 · 1 comment

Comments

@adamSales
Copy link

I came across an issue, and wrote a small hack to circumvent it that seems to work--here goes:

When running a model with nrep>1, I'll sometimes get output that looks like this:

...
Model 78: llik = -22780 ... best llik = -19583
Model 79: llik = -21839 ... best llik = -19583
Error in poLCA:::poLCA.ylik.C(vp, y) :
NA/NaN/Inf in foreign function call (arg 1)

I'm not entirely sure what the error is, but (since the model fit the first 79 times) I assume it has to do with bad randomly-chosen starting values. In this situation, I'd rather the software just discard the bad result and move on, rather than crash entirely. By tweaking the poLCA code as follows, it does just that.
First, a little function (I bet there's a more elegant way of doing this):

tryNA <- function(x){
    x <- try(x)
    if(inherits(x,'try-error')) return(NA)
    x
}

the replace the line

llik[iter] <- sum(log(rowSums(prior *poLCA.ylik.C(vp,`
                    y))))

with

llik[iter] <- tryNA(sum(log(rowSums(prior *poLCA.ylik.C(vp,
                      y)))))

and now everything seems to work just fine.

@jibnew
Copy link

jibnew commented Dec 4, 2024

Why not incorporate this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants