what is syntax for using iter function - r

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.

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.

collect output of system rscript command as r object

I am using RStudio in Windows to develop and run a pipeline for multivariate analysis that involve big dataset (90 by ~ 60000 matrices). With matrices of such a size, I got "protection from stack overflow" pretty often. One way of avoid this problem, while still using RStudio as opposite to the regular Rgui, is to run my script(s) using the following syntax
system("Rscript --max-ppsize=500000, my_script.s").
However, running this command results in the script running succesfully, but I cannot get the desired output. If I run the previous command with the following option
opt-< system("Rscript --max-ppsize=500000, my_script.s", internal = TRUE)
I got the standard output to the terminal as output (as a character vector), but not the desired output.
Consider this toy examples:
save the following code in my_script.R
print("first call")
rnorm(15)
print("second call")
rnorm(20)
and run the following code from the console
a <- system("Rscript my_script.R", intern = TRUE)
a
As you can see, the output is a character vector of length 9 with the standard output to the console.
If you modify my_script.R as follow
print("first call")
i_want_this <- rnorm(20)
and then run it again
a <- system("Rscript my_script.R", intern = TRUE)
a
now the only thing stored in a is the output of the print command.
My question is: is there a way to collect the i_want_this variable as an r object (in this case a numeric vector of length 20) ?
A similar question has been asked here, without a satisfying answer.

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?

Retrieving expected data.frame for testthat expectation

I'd like to test that a function returns the expected data.frame. The data.frame is too large to define in the R file (eg, using something like structure()). I'm doing something wrong with the environments when I try a simple retrieval from disk, like:
test_that("SO example for data.frame retreival", {
path_expected <- "./inst/test_data/project_longitudinal/expected/default.rds"
actual <- data.frame(a=1:5, b=6:10) #saveRDS(actual, file=path_expected)
expected <- readRDS(path_expected)
expect_equal(actual, expected, label="The returned data.frame should be correct")
})
The lines execute correctly when run in the console. But when I run devtools::test(), the following error occurs when the rds/data.frame is read from a file.
1. Error: All Records -Default ----------------------------------------------------------------
cannot open the connection
1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls, message = function(c) invokeRestart("muffleMessage"),
warning = function(c) invokeRestart("muffleWarning"))
2: eval(code, new_test_environment)
3: eval(expr, envir, enclos)
4: readRDS(path_expected) at test-read_batch_longitudinal.R:59
5: gzfile(file, "rb")
To make this work, what adjustments are necessary to the environment? If there's not an easy way, what's a good way to test large data.frames?
I suggest you check out the excellent ensurer package. You can include these functions inside the function itself (rather than as part of the testthat test set).
It will throw an error if the dataframe (or whatever object you'd like to check) doesn't fulfill your requirements, and will just return the object if it passes your tests.
The difference with testthat is that ensurer is built to check your objects at runtime, which probably circumvents the entire environment problem you are facing, as the object is tested inside the function at runtime.
See the end of this vignette, to see how to test the dataframe against a template that you can make as detailed as you like. You'll also find many other tests you can run inside the function. It looks like this approach may be preferable over testthat in this case.
Based on the comment by #Gavin Simpson, the problem didn't involve environments, but instead the file path. Changing the snippet's second line worked.
path_qualified <- base::file.path(
devtools::inst(name="REDCapR"),
test_data/project_longitudinal/expected/dummy.rds"
)
The file's location is found whether I'm debugging interactively, or testthat is running (and thus whether inst is in the path or not).

R function optim(): object not found

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

Resources