R错误:承诺已经在评估中

时间:2017-07-08 12:45:26

标签: r recursion

任何人都可以解释如何修复此错误?我意识到我没有提供所有代码。数据来自着名的天气高尔夫数据。我得到的错误是probPCNB中的错误(varClass =" play",valueClass = i,inputVar1 =" windy",:   承诺已经在评估中:递归默认参数引用还是早期问题?我已经看到了这类问题的一些答案,但我并不完全理解它们,特别是在这种情况下。虽然还有其他问题可以解决这个错误,但在这种情况下错误的来源并不清楚。

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=df){
  ##probPCNB(varClass='play', valueClass='yes', inputVar1='windy',var1Level='TRUE',inputVar2='humidity',var2Level="high",df)
  #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)
    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()

1 个答案:

答案 0 :(得分:1)

这是因为参数df = df

将df重命名为其他内容,它应该可以正常工作

另见: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()