Promise Already Under Evaluation: Recursive Default Argument Reference or Earlier Problems

promise already under evaluation: recursive default argument reference or earlier problems?

Formal arguments of the form x=x cause this. Eliminating the two instances where they occur we get the following. (The reason you can't use x=x in the formal arguments of a function definition is that it first looks up the default argument within the function itself so using that form is telling it to use itself as the default but it has not been defined so that makes no sense and we get an error.)

f <- function(x, T) {
10 * sin(0.3 * x) * sin(1.3 * x^2) + 0.001 * x^3 + 0.2 * x + 80
}

g <- function(x, T, f. = f) { ## 1. note f.
exp(-f.(x)/T)
}

test<- function(g. = g, T = 1) { ## 2. note g.
g.(1,T)
}

test()
## [1] 8.560335e-37

R error: promise already under evaluation

It's because of the argument df=df

Rename df to something else and it should work

Also see: promise already under evaluation: recursive default argument reference or earlier problems?

golf=read.csv(file="weather.nominal.csv")
df=as.data.frame(golf)
df$names<-rownames(df)


conditional_prob_bool <- function(spec11, spec12, spec21, spec22, df){

consequent_bool <- df[,spec11] == spec12

consequent_event <- df[consequent_bool,]

antecedent_bool <- df[,spec21] == spec22
antecedent_event=df[antecedent_bool,]

return(prob(intersect_event(consequent_event,antecedent_event,df),df)/prob(antecedent_event,df))
}



predNBVar2=function(varClass='play',inputVar1='windy',var1Level='TRUE',inputVar2='humidity',var2Level="high",df_predNBVar2=df){
##probPCNB(varClass='play', valueClass='yes', inputVar1='windy',var1Level='TRUE',inputVar2='humidity',var2Level="high",df_predNBVar2)
#browser()
#lev2=levels(df[,varClass])
lev2=c('yes','no')
#lev2=levels(df$varClass)
countt=0
classprob=0
predictedclass=lev2[1]
for(i in lev2){
p=probPCNB(varClass='play', valueClass=i, inputVar1='windy',var1Level='TRUE',inputVar2='humidity',var2Level="high",df=df_predNBVar2)
if(classprob<p){
classprob=p
predictedclass=i
}


}
return(paste("best",predictedclass," with measure",classprob ))

}




probPCNB=function(varClass='play', valueClass='true', inputVar1='windy',var1Level=TRUE,inputVar2='humidity',var2Level='high',df){
#browser()

A_bool=df[,inputVar1]==var1Level & df[,inputVar2]==var2Level
A=df[A_bool,]

class_bool=df[,varClass]==valueClass
classE=df[class_bool,]


numerator=prob(classE,df)*conditional_prob_bool(inputVar1, var1Level,varClass,valueClass,df)*conditional_prob_bool(inputVar2, var2Level,varClass,valueClass,df)


lev=levels(df[,varClass])
denominator=0

for(i in lev){

class_bool=df[,varClass]==i

classE=df[class_bool,]
class=i
denominator= denominator+(prob(classE,df)*conditional_prob_bool(inputVar1, var1Level,varClass,class,df)*
conditional_prob_bool(inputVar2, var2Level,varClass,class,df))

}
return(numerator/denominator)

}

predNBVar2()

How to avoid promise already under evaluation warning for setting default argument as a function of another argument

One would need to look deep into R's (notorious) environments to understand exactly, where it tries to find ones. The problem is located in the way supplied and default arguments are evaluated within a function. You can see this link from the R manual and also an explanation here.

The easy solution is to tell R where to look for it. It will save you the hassle. In your case that's the global environment.

Changing method myfun.character to tell it to look for ones in the global environment:

myfun.character <- function(x, ones = get('ones', envir = globalenv())(x)) {

myfun(as.list(x), ones)

}

will be enough here.

Out:

myfun(letters[1:5])
# x ones
#1 a 1
#2 a 1
#3 a 1
#4 a 1
#5 a 1

myfun(letters[1:5], 1:5)
# x ones
#1 a 1
#2 a 2
#3 a 3
#4 a 4
#5 a 5

R: promise already under evaluation

When f is called a stack of 5 levels is built down to show.large.objects, which starts to evaluate the contents of the frames starting from the top.

f
-> print
-> system.time
-> show.large.objects.stack
-> show.large.objects

Level 1

f()

Everything ok here.

Level 2

print(system.time(show.large.objects.stack()))

When you call ls(.envir, all.names) on its frame you get

[1] "..." "x"  

of which ... is missing and throws error 3 when you call get on it, and x = system.time(show.large.objects.stack()) is currently being evaluated and throws error 4.

Level 3

system.time(show.large.objects.stack())

whose ls gives you

[1] "expr"    "gcFirst" "ppt"     "time"   

of which expr = show.large.objects.stack() is still currently being evaluated and throws another of error 4.

Level 4

show.large.objects.stack()

whose ls contain no sketchy things and completes without errors.

Bottom line

show.large.frames() must be evalutad on its own, not as an argument to any function, or it will throw errors. Why not letting it do the printing itself?

I found this very helpful

> debug(show.large.objects)
> f()
Browse[2]> lapply(sys.frames(), ls)
[[1]]
[1] "c" "d"

[[2]]
[1] "x"

[[3]]
[1] "expr" "gcFirst" "ppt" "time"

[[4]]
[1] "level" "skip.levels"

[[5]]
[1] "exclude" "threshold"

An error I can't understand. Promise already under evaluation...

Someone can correct me on this, but I think that you are passing Primus and Name as objects to the function and it is looking in the .GlobalEnv for those objects and is not finding them, therefore your function is failing to carry out most of your instructions (and is returning nothing). I have edited your function a bit.

Instead try this...

 namezz <- function( pattern = " ", data , column= "Name" ){
library(stringr)
strings <- data[ , column ] ##data$column is a character vector
found = str_detect( strings , pattern )
yez = rownames( data[ which( found==TRUE ) , ] )
hhh = as.numeric( yez ) + 1
return( hhh )
}

Then you must use the function like so:

namezz( "Primus" , data = data ) #In this case the default for column is "Name" as you want

The problem with passing data = data is explained very nicely here. An excerpt from that post (where they refer to testparams you would refer to data)...

"One of the most important things to know about the evaluation of
arguments to a function is
that supplied arguments and default arguments are treated differently.
The supplied arguments
to a function are evaluated in the evaluation frame of the calling
function. The default arguments
to a function are evaluated in the evaluation frame of the function."

the parameter testparams, when no matching argument is passed, is given
the default value which is the value of the variable testparams
looked-up not in the environment where foo is defined, and not in
the environment where foo is called, but rather in the local environment
created when the function is called and where parameters are mapped to
values -- and in this environment, testparams is a parameter, which is
already being under evaluation, hence the recursive lookup error.

promise already under evaluation error in R caret's rfe function

There are two problems with your code.

  1. You need to specify the function/algorithm that you want to fit. (this is what causes the error message you get. I am unsure why rfe throws such a cryptic error message; it makes it difficult to debug, indeed.)
  2. You need to name your columns in the input data.

The following works:

library(caret)

X_values = c(29.04,96.57,4.57,94.23,66.81,26.71,69.01,77.06,49.52,97.59,47.57,64.07,24.25,11.27,77.30,90.99,44.05,30.96,96.32,16.04)
X = matrix(X_values, nrow = 5, ncol=4)
Y = c(5608.11,2916.61,5093.05,3949.35,2482.52)

ctrl <- rfeControl(functions = lmFuncs)
colnames(X) <- letters[1:ncol(X)]

set.seed(123)
rfe(X, Y, rfeControl = ctrl)

I chose a linear model for the rfe.
The reason for the warning messages is the low number of observations in your data during cross validation. You probably also want to set the sizes argument to get a meaningful feature elimination.

R error promise already under evaluation when using subset in function but no error in script

It's your recursive use of TypeIDs. The thing to remember is that function arguments are lazily evaluated, which allows cool stuff like function(foo, bar = foo). Unfortunately in this case setting the default for TypeIDs to itself causes a recursion in the evaluation. Try changing the name of either the parameter or the outer object.



Related Topics



Leave a reply



Submit