What is the sense of the is.single() function in R? - r

I stumbled on the function is.single() and do not understand what is does. The description says:
is.single reports an error. There are no single precision values in R.
And indeed, if I try to test different things inside the function I get an error. Example:
is.single(1)
Error in is.single(1) : type "single" unimplemented in R
I see no sense in a function that seemingly always gives an error. What is the function for?

Related

Parsing error in MonteCarlo::MonteCarlo function in R

I am trying to run a power analysis using a MonteCarlo approach in R.
I have created a function of two parameters that does output a boolean (tested manually for all relevant values of the parameters). I also have run baby-examples of the MonteCarlo function to make sure that I understand it and that it works well.
Yet when I try to run the real thing, I get the following error message:
Error in parse(text = all_funcs_found[i]) : <text>:1:1: unexpected '::'
1: ::
I read through the source code of the MonteCarlo function (which I found here) and found
#loop through non-primitive functions used in func and check from which package they are
for(i in 1:length(all_funcs_found)){
if(environmentName(environment(eval(parse(text=all_funcs_found[i]))))%in%env_names){
packages<-c(packages,env_names[which(env_names==environmentName(environment(eval(parse(text=all_funcs_found[i])))))])
}
}
which doesn't really make sense to me - why should there be a problem there?
Thank you for any ideas.
I found the answer: the function I wrote was calling a function from a specific library in the form libraryname::functionname.
This works OK if you use the function once manually, but makes MonteCarlo break.
I solved the problem by first loading the relevant library, then removing the 'libraryname::' part from the definition of the main function. MonteCarlo then runs just fine.

What causes this? rdplot Error in R: Error in seq.default(x_min, c, jump_l) : invalid '(to - from)/by'

I am trying to use some very simple data in rdplot but I keep getting the error: Error in seq.default(x_min, c, jump_l) : invalid '(to - from)/by' I found this response from a diffrent post on Stack Overflow but can seem to apply the fix to the rdplot function. does anyone know how can this be fixed?
The actual code I am using is:
library(rdrobust)
rdplot(y = dt$treated, x = dt$score)
Reproducibility:
Here is a sample of my data, as I've said it is fairly common data. So far I have found a couple of things:
There are multiple subsets of the observations that seem to cause problems
Changing the nbins or binselect arguments from their default will fix the problem
The first time you get an error is on the set 1:1463, however using only observations 2:1464 is OK.
The recent rdrobust version 1.0.1 should take care of this error.

What R package is the "Error" function in?

I'm trying to call the Error() function but it says could not find function "Error". I checked the docs and Error does not seem to be a part of R base package. This is a very hard function to search for because "Error" is a very overloaded word. What package is Error() in? For context, I'm running an anova. I'm pretty sure that this isn't a user defined since I see multiple tutorials referencing it without defining.
EDIT:
Here are the tutorials:
https://datascienceplus.com/two-way-anova-with-repeated-measures/ , http://personality-project.org/r/r.guide/r.anova.html#withinone (look at usages of Error() in within sujects/repeated measures anova)
EDIT2:
Here is the model answer from the tutorial. There does not seem to be any information about how the 'Error' function is defined or where it comes from:
model <- aov(wm$iq ~ wm$condition + Error(wm$subject / wm$condition))
The Error() in this case is specifying the error term for the aov function. It's a parameter passed to the function aov() and thus is not a function on its own. I've also tried searching for Error using the package sos, which yields 0 results:
# install.packages("sos")
library(sos)
results <- findFn("Error")
filtered_results <- results[results$Function == 'Error']
nrow(filtered_results)
Output:
[1] 0
You might want to read this Cross Validated post on how to set the Error term within the aov() function.

Object not found when called within a function with a formula

I'm trying to call the checkm function from within another function that accepts a formula as a parameter. I'm getting an object not found error. Here is the minimal implementation and error.
library(lrmest)
data(pcd)
form<-formula(Y~X1+X2+X3+X4)
checkm(form,data=pcd)
Wrap<-function(f){
checkm(f,data=pcd)
}
Wrap(form)
The error is:
Error in model.frame(formula = f, data = pcd, NULL) :
object 'f' not found
Called from: eval(expr, envir, enclos)
My guess from reading around is this has to do with my not understanding environments or promises but given that I don't understand them, I'm probably wrong.
Any quick fixes?
One quick fix is to change the name of your formula argument. It happens to conflict with the eval(cal) call within checkm. I suspect #joran is right that this isn't your fault. This works:
library(lrmest)
data(pcd)
form<-Y~X1+X2+X3+X4
checkm(form,data=pcd)
Wrap<-function(formula){
checkm(formula,data=pcd)
}
Wrap(form)
As #joran pointed out, there is a bug/error in the function caused from not using the correct frame to evaluate the command. If you swap out checkm for lm you'll see it runs just fine. You can create your own function that changes just that one line of code with
checkm2<-checkm
body(checkm2)[[6]]<-quote(cal <- eval(cal, parent.frame()))
And then run
library(lrmest)
data(pcd)
form<-formula(Y~X1+X2+X3+X4)
checkm2(form,data=pcd)
Wrap<-function(f){
checkm2(f,data=pcd)
}
Wrap(form)
and everything seems to run properly. So that just appears to be the fault of the people who wrote the code. You might consider contacting them to file a bug report.

R 'object XX not found' error thrown inside function, but not in script

I am fairly new to R, so my apologies if this question is a bit silly.
I am calling a function in an external package ('mmlcr', although I don't think that is directly relevant to my problem), and one of the required inputs (data) is a data.frame. I compose the data.frame from various data using the following approach (simplified for illustration):
#id, Time, and value are vectors created elsewhere in the code.
myData = data.frame(a=id, b=Time, c=value)
out <- mmlcr( input1, input2, data=myData, input4)
Which throws the error:
Error in is.data.frame(data) : object 'myData' not found
The debugger indicates that this error is thrown during the mmlcr() call.
I then added a print(ls()) immediately prior to the mmlcr() call, and the output confirmed that "myData" was in my function workspace; further is.data.frame(myData) returned TRUE. So it seems that "myData" is successfully being created, but for some reason it is not passing into the mmlcr() function properly. (Commenting this line causes no error to be thrown, so I'm pretty sure this is the problematic line).
However, when I put the exact same code in a script (i.e., not within a function block), no such error is thrown and the output is as expected. Thus, I assume there is some scoping issue that arises.
I have tried both assignment approaches:
myData = data.frame(a=id, b=Time, c=value)
myData <- data.frame(a=id, b=Time, c=value)
and both give me the same error. I admit that I don't fully understand the scope model in R (I've read about the differences between = and <- and I think I get it, but I'm not sure).
Any advice you can offer would be appreciated.
MMLCR is now deprecated and you should search for some alternatives. Without looking too much into it, I sleuthed through an old repo and found the culprit:
m <- eval(m, data)
in the function mmlcr.default. There are a lot of reasons why this is bad, but scoping is the big one. R has this issue with the subset.data.frame function, see my old SO question. Rather than modify the source code, I would find a way to do your function with a subroutine using a for, repeat, or while loop.

Resources