r msm BLAS/LAPACK routine 'DGEBAL' gave error code -3 - r

I'm trying to make a basic markov model using the package msm and things were working fine until I've suddenly started receiving the following error code. I don't know why it's suddenly started throwing this as it was working fine earlier, and I don't think I've changed anything. The error code seems to be pointing to the linear algebra library but I don't know what to do with it exactly ...
Error in balance(baP$z, "S") :
BLAS/LAPACK routine 'DGEBAL' gave error code -3
The code is as follows:
statesDistMatrix2 <- matrix(c(.1,0,0,.1), nrow = 2, ncol = 2)
msm1 <- msm(error ~ stop_datetime, subject = TRIP_ID, data = train_245_mk,
qmatrix = statesDistMatrix2, control=list(fnscale=5000,maxit=500))

From this document http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.230.5929&rep=rep1&type=pdf about LAPACK
It seems that (section Error Indicators and Warnings)
"Errors or warnings detected by the routine: INFO < 0
If INFO = -i, the ith parameter had an illegal value."
Still from this document, the third parameter which seems to be the problem in your case has to be real. Chances are that some complex value appears at some point in the process. Make sure that this parameter only has real values. Sometimes, some operations can lead to results such as 1 + 0.000000001j and this is problematic, try to call the function by taking only the real part of the argument you pass in.
Hope this helps.

Related

When do you use warning(msg)?

I am a little confused about the reason why R has warning(msg). It seems it doesn't really cause anything to happen during code execution except store some messages in warnings() that no one ever really looks at.
Maybe I am using it wrong. Maybe I am misunderstanding its purpose. No other language I used has a warning exception, most just have an exception (I guess thie equates to an error(msg) in R). Is warning(msg) an exception that I just ignore, then what's the point of it?
Maybe someone has a good explanation of when you SHOULD use a warning(msg)? or what it was intended to be used for?
As far as I understand it, warning() is an alternative to message() not necessarily to stop() which causes an error. In a warning a user is informed that something is not as expected but it is left to the user to decide if the problem is severe and if they want to re-write their code.
The main difference between message() and warning() is that a warning message will lead with Warning message: and that R keeps track if the latter one occurs. You can choose for yourself if you want your code to completely stop on any warning as well by setting
options(warn = 2)
One usage I can think of is to warn the user that a function s/he is using will be deprecated soon.Many users will choose run the code anyway and fix it later on when they have time. But they definitely know now there is a problem in the future.
Another use case would be to make the user aware of a problem that was fixed internally. In this case the user should know that the function did more than the user asked for, in case the fix is not what they were trying to do. An example from dplyr would be this:
> a <- data.frame(x = c("p", "q", NA), y = c(1, 2, 3), stringsAsFactors = TRUE)
> b <- data.frame(x = c("p", "q", "r"), z = c(4, 5, 6), stringsAsFactors = TRUE)
> res <- inner_join(a, b, "x")
Warning message:
Column `x` joining factors with different levels, coercing to character vector
This could mean there is a severe issue or absolutely nothing, depending on your use case.
You could choose to stop the function if you prefer that users get it right on their own. And in fact some people advocate for that principle. I usually try to make my packages as user-friendly as possible so people don't have to struggle with an error they don't understand. At the same time I want to inform them they might have made a common mistake could opt to fix it if it was not what they intended to do.
In response to : It seems it doesn't really cause anything to happen during code execution except store some messages in warnings() that no one ever really looks at., try running this:
why_warning<-function(x){
if(is.numeric(x)){
warning("x must be a string")
}
print(x)
}
Running this with:
why_warning(2)
We get:
[1] 2
Warning message:
In why_warning(2) : x must be a string
In short, warning is intended to acknowledge that there are problems but they have been "ignored" and a result returned. This is particularly useful for numerical computations.
Compare this to the function below:
why_warning<-function(x){
stopifnot(is.character(x))
print(x)
}
The above will not "return" any value but rather stop and throw the error:
Error in why_warning(2) : is.character(x) is not TRUE
Therefore, depending on the goal of the function, a warning may be just that: a warning. However, it may mean that whatever results one obtains are erroneous.
You can find more details on warning under: ?warning.

r extension netlogo, object 'economicvalue' not found

I am working in netlogo on a model which has to communicate with R during the run. I do this using the r extension in netlogo (so not Rnetlogo in R).
at the setup I load my script with
r:eval "source('C:/Users/keemi/OneDrive/Documenten/Thesis/heatpumps/scriptHeatpumpV1.R')"
this works fine since I can ask what I want coming from the script with this code. r:get "cpquery(fittedHeatpumpv1, event = (Reliability == 0.88), evidence = (Economic == 0.08))" this gives me a chance percentage of the event given the evidence.
However the evidence must come from the netlogo network, I do this using
r:put "economicvalue" reliability this creates a variable in r -> economicvalue from the value of reliability in netlogo (which is 0.08 for the example).
I then put in the following code r:get "cpquery(fittedHeatpumpv1, event = (Reliability == 0.88), evidence = (Economic == economicvalue))" to get to the same result, however netlogo gives the error
Extension exception: Error in R-Extension: Error in Get.
org.nlogo.api.ExtensionException: Error in eval(evidence, generated.data, parent.frame()) :
object 'economicvalue' not found
error while company 157 running R:GET
called by procedure INVEST
called by procedure GO
called by Button 'go-once'
this is odd since if I do the same thing in r itself it works just fine. and the script itself also works fine since I can load things from it.
I also checked the value of the r:put and this was indeed set to 0.08 if I call it back using r:get "economicvalue"
I also tested it already without the variable coming from netlogo but just giving the command directly to r using r:eval "economicvalue <- 0.08"but the same error occurs.
I can't figure out what I am doing wrong here, since the code works in r itself if I put the same code lines but not coming from netlogo, and netlogo also performs well since I can see if the r commands work with the r:get and this all gives the right values.
could somebody help me out?

Error in R code for Marshal Olkin Bivariate Exponential distribution

What does this error mean?
Error in `[<-`(`*tmp*`, i, 1, value = 0.0225315561703551) :
subscript out of bounds
This error code means you are trying to index your variable outside its range. Example so if you had an array x <- c(1,2,3) and you were wanted x[4], or tried to call x[3.14].
Check your code, To debug in Rstudio, I put browser() statements in where I want code to stop and then step through the process.
Check how you are indexing any loops. I noticed it is complaining about i
Update your package you are using for calc. You can sometimes run into gremlins that have been fixed in later versions.
This may be helpful: Subscript out of bounds - general definition and solution?

Dirichlet-Categorical conjugate prior model using OpenBUGS,R and the package R2OpenBUGS

At first, let's create some sample categorical data with 3 levels.
y<-sample(c("A","B","C"),50,replace=TRUE)
I'm trying to formulate a Bayesian statistical model in which the y variable follows categorical distribution with parameters theta1,theta2,theta3. These parameters describe the probability a single y[i] belongs to the corresponding category. In the bayesian perspective, these parameters are also random variables and we use to assign a dirichlet prior to them with hyper-parameters alpha1,alpha2,alpha3.
I'm having some problems with the syntax as it seems.
CODE
model<-function(){
#likelihood
for( i in 1:N){
y[i]~ dcat(theta[])
}
#prior
theta[1:3]~ ddirch(alpha[])
}
library(R2OpenBUGS)
model.file <- file.path(tempdir(),"model.txt")
write.model(model, model.file)
y<-sample(c("A","B","C"),50,replace=TRUE)
N<-50
alpha<-c(1,1,1)
data<-list('y','N','alpha')
params<-c('theta')
inits<-function(){theta=c(1/3,1/3,1/3)}
We call OpenBUGS through R, with the bugs function
out<-bugs(data,inits,params,model.file,n.chains = 2
,n.iter=6000,codaPkg = TRUE,n.burnin = 1000,DIC = TRUE)
I've tried different ways to syntactically formulate the above code, dribbling through the errors and getting familiar with the log.txt file (that is the file that keeps the OpenBUGS output) until this code gave me a log.txt with no errors while R still has problems.
R output
Error in bugs.run(n.burnin, OpenBUGS.pgm, debug = debug, WINE = WINE, :
Look at the log file in /tmp/Rtmpofdk0t and
try again with 'debug=TRUE' to figure out what went wrong within OpenBUGS.
In addition: Warning message:
In FUN(X[[i]], ...) : class of 'x' was discarded
log.txt
OpenBUGS version 3.2.3 rev 1012
model is syntactically correct
data loaded
model compiled
initial values generated, model initialized
1000 updates took 0 s
monitor set
monitor set
monitor set
monitor set
deviance set
Thanks in advance for your help
I think you should rename theta1, theta2, theta3 with alpha1, alpha2, alpha3, because you use the alpha1,... in the function ddirch, but you never declare them. Instead you declare theta1 and so on, but never use them.
If there are any other issues, you might have a look at the log file, like the compiler suggests.
After numerous experiments, i figured out that for some reason OpenBUGS cant accept factor variables given as usual. So i changed my data ( format "A","B","C") to numeric (format 1,2,3) with the as.numeric R function and everything ran smoothly!

R - Rstudio - make R play a sound if warning/error generated

I am running a script that loops over a list of stock pair combinations... occasionally the script stops running due to an error generated by differing data lengths between pair combo and I simply remove the mismatched stock from consideration):
Error in model.frame.default(formula = stckY ~ stckX + 0, drop.unused.levels = TRUE) :
variable lengths differ (found for 'stckX')
Is there any way I can make R / Rstudio play a sound when the error message occurs so that I can be alerted without having to keep my eyes on the screen while the script is looping along?
I can generate sounds linearly using:
beep <- function(n = 3){
for(i in seq(n)){
system("rundll32 user32.dll,MessageBeep -1")
Sys.sleep(.5)
}
}
beep()
but how can I do this conditional on an error message?
Building on #frankc answer and #hrbrmstr comment, a way to do this:
install.packages("beepr")
library(beepr)
options(error = beep)
try options(error = beep)
you would still need to define beep before you do this. Haven't verified this works but it should per ?options:
'error': either a function or an expression governing the handling
of non-catastrophic errors such as those generated by 'stop'
as well as by signals and internally detected errors. If the
option is a function, a call to that function, with no
arguments, is generated as the expression. The default value
is 'NULL': see 'stop' for the behaviour in that case. The
functions 'dump.frames' and 'recover' provide alternatives
that allow post-mortem debugging. Note that these need to
specified as e.g. 'options(error = utils::recover)' in
startup files such as '.Rprofile'.

Resources