R function optim(): object not found - r

So I've been working on a script to calculate log likelihood based on specifically 4 parameters and put them into a particular log-lik function. That script is fine. The issue is optimizing it - whenever I try, it says that the object (for the parameter in question) can not be found. For the sake of simplicity, I will just use a much simpler script that is giving me an identical error when I use the optim() function. The funny part is that I actually pulled this script directly out of a "MLE in R" tutorial. I didn't even rewrite it, I literally copied/pasted it from the PDF. Here is the likelihood function from the tutorial:
normal.lik1<-function(theta,y){
mu<-theta[1]
sigma2<-theta[2]
n<-nrow(y)
logl<- -.5*n*log(2*pi) -.5*n*log(sigma2) - (1/(2*sigma2))*sum((y-mu)**2)
return(-logl)
}
This function works perfectly fine on it's own. However, when I try to optimize it, I get an error saying that the object is not found, the object being the parameter I'm trying to optimize. This next line is also copy/pasted from the tutorial:
optim(c(0,1),normal.lik1,y=y,method="BFGS")
When I run this line, R gives me the following error:
Error in nrow(y) : object 'y' not found
That immediately made me realize that it's talking about how y is an object specifically within the function, not a global one, but at the same time optim() is supposed to be evaluating that very function! So, I don't know why it is acting like y doesn't exist.
EDIT:
Now I understand how it works. But I am still having an issue with my overall goal. To put it more into perspective, here is the code I am working with now:
w.loglik<-function(w){
# w is just a vector with 4 values: two means (w(1) and w(2) below) and the position
# of two decision bounds (w(3) and w(4))
#create data matrix
data<-matrix(c(140,36,34,40,89,91,4,66,85,5,90,70,20,59,8,163),nrow=4,ncol=4,byrow=TRUE)
# get means
means<-matrix(0,4,2,byrow=TRUE)
means[2,1]<-w[1]
means[3,2]<-w[2]
means[4,1]<-w[1]
means[4,2]<-w[2]
# get covariance matrices (fix variances to 1)
covmat<-list()
covmat[[1]]<-diag(2)
covmat[[2]]<-diag(2)
covmat[[3]]<-diag(2)
covmat[[4]]<-diag(2)
# get decision bound parameters
b<-diag(2)
c<-matrix(c(w[3],w[4]),2,1)
L<-matrixloglik(data,means,covmat,b,c)
return(L)
}
matrixloglik is just a function that outputs a log likelihood (it runs fine). How can I run optim() so that I optimize the vector w?

y is your data and you haven't specified that in your code. Hence the error
Error in nrow(y) : object 'y' not found
If you go the example 5 of same pdf file and use the data y as defined below, you will have an output:
X<-cbind(1,runif(100))
theta.true<-c(2,3,1)
y<-X%*%theta.true[1:2] + rnorm(100)
> optim(c(0,1),normal.lik1,y=y,method="BFGS")
$par
[1] 3.507266 1.980783
$value
[1] 176.0685
$counts
function gradient
49 23
$convergence
[1] 0
$message
NULL
Warning messages:
1: In log(sigma2) : NaNs produced
2: In log(sigma2) : NaNs produced
3: In log(sigma2) : NaNs produced
Note: Please see the "An Introduction to R" to understand the basics in function.
Update: as per comments: You can try doing and see what happens:
y<-X%*%theta.true++ rnorm(100)
Error in X %*% theta.true : non-conformable arguments

Related

Error: object 'skim_without_charts' not found [duplicate]

I got the error message:
Error: object 'x' not found
Or a more complex version like
Error in mean(x) :
error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found
What does this mean?
The error means that R could not find the variable mentioned in the error message.
The easiest way to reproduce the error is to type the name of a variable that doesn't exist. (If you've defined x already, use a different variable name.)
x
## Error: object 'x' not found
The more complex version of the error has the same cause: calling a function when x does not exist.
mean(x)
## Error in mean(x) :
## error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found
Once the variable has been defined, the error will not occur.
x <- 1:5
x
## [1] 1 2 3 4 5
mean(x)
## [1] 3
You can check to see if a variable exists using ls or exists.
ls() # lists all the variables that have been defined
exists("x") # returns TRUE or FALSE, depending upon whether x has been defined.
Errors like this can occur when you are using non-standard evaluation. For example, when using subset, the error will occur if a column name is not present in the data frame to subset.
d <- data.frame(a = rnorm(5))
subset(d, b > 0)
## Error in eval(expr, envir, enclos) : object 'b' not found
The error can also occur if you use custom evaluation.
get("var", "package:stats") #returns the var function
get("var", "package:utils")
## Error in get("var", "package:utils") : object 'var' not found
In the second case, the var function cannot be found when R looks in the utils package's environment because utils is further down the search list than stats.
In more advanced use cases, you may wish to read:
The Scope section of the CRAN manual Intro to R and demo(scoping)
The Non-standard evaluation chapter of Advanced R
While executing multiple lines of code in R, you need to first select all the lines of code and then click on "Run".
This error usually comes up when we don't select our statements and click on "Run".
Let's discuss why an "object not found" error can be thrown in R in addition to explaining what it means. What it means (to many) is obvious: the variable in question, at least according to the R interpreter, has not yet been defined, but if you see your object in your code there can be multiple reasons for why this is happening:
check syntax of your declarations. If you mis-typed even one letter or used upper case instead of lower case in a later calling statement, then it won't match your original declaration and this error will occur.
Are you getting this error in a notebook or markdown document? You may simply need to re-run an earlier cell that has your declarations before running the current cell where you are calling the variable.
Are you trying to knit your R document and the variable works find when you run the cells but not when you knit the cells? If so - then you want to examine the snippet I am providing below for a possible side effect that triggers this error:
{r sourceDataProb1, echo=F, eval=F}
# some code here
The above snippet is from the beginning of an R markdown cell. If eval and echo are both set to False this can trigger an error when you try to knit the document. To clarify. I had a use case where I had left these flags as False because I thought i did not want my code echoed or its results to show in the markdown HTML I was generating. But since the variable was then used in later cells, this caused an error during knitting. Simple trial and error with T/F TRUE/FALSE flags can establish if this is the source of your error when it occurs in knitting an R markdown document from RStudio.
Lastly: did you remove the variable or clear it from memory after declaring it?
rm() removes the variable
hitting the broom icon in the evironment window of RStudio clearls everything in the current working environment
ls() can help you see what is active right now to look for a missing declaration.
exists("x") - as mentioned by another poster, can help you test a specific value in an environment with a very lengthy list of active variables
I had a similar problem with R-studio. When I tried to do my plots, this message was showing up.
Eventually I realised that the reason behind this was that my "window" for the plots was too small, and I had to make it bigger to "fit" all the plots inside!
Hope to help
I'm going to add this on here even though it's not a new question as it comes quite highly in the search results for the error:
As mentioned above, re checking syntax, if you're using dplyr, make sure you have all the %>% pipes at the end of the lines above the error, otherwise the contents of anything like a select statement won't pass down into the next part of the code block.

what is syntax for using iter function

I have no documentation or knowledge of how to run a iter command. The question is as follows:
Run the kc$iter command. Include the command, output screenshot, and
explain what the output shows.
# set seed
set.seed(12345)
# Cluster analysis
kc<-kmeans(myvehicle, 4)
kc
# Iteration
kc$iter(kc(myvehicle), )
The last command is a guess and I got an error message: Error: attempt to apply non-function. I don't know what the syntax is and the example I used and it had a value of 12 after the comma. However, I don't know why so I left it blank. I couldn't find anything that describes how to use this function.

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

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.

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!

Resources