1. ホーム
  2. r

[解決済み] Rエラー:約束はすでに評価中です[重複]。

2022-02-09 09:12:30

質問

このエラーを修正する方法をどなたか教えてください。私はすべてのコードを提供していないことを認識しています。データはよく知られている天気予報のゴルフデータからです。私が得ているエラーは エラー in probPCNB(varClass = "play", valueClass = i, inputVar1 = "windy", : 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=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()

解決方法は?

引数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()