Predict method for the nb function

# S3 method for nb
predict(
  object,
  pred_data = NULL,
  pred_cmd = "",
  pred_names = "",
  dec = 3,
  envir = parent.frame(),
  ...
)

Arguments

object

Return value from nb

pred_data

Provide the dataframe to generate predictions (e.g., titanic). The dataset must contain all columns used in the estimation

pred_cmd

Generate predictions using a command. For example, `pclass = levels(pclass)` would produce predictions for the different levels of factor `pclass`. To add another variable, create a vector of prediction strings, (e.g., c('pclass = levels(pclass)', 'age = seq(0,100,20)')

pred_names

Names for the predictions to be stored. If one name is provided, only the first column of predictions is stored. If empty, the level in the response variable of the nb model will be used

dec

Number of decimals to show

envir

Environment to extract data from

...

further arguments passed to or from other methods

Details

See https://radiant-rstats.github.io/docs/model/nb.html for an example in Radiant

See also

nb to generate the result

summary.nb to summarize results

Examples

result <- nb(titanic, "survived", c("pclass", "sex", "age")) predict(result, pred_data = titanic)
#> Naive Bayes Classifier #> Data : titanic #> Response variable : survived #> Level(s) : Yes, No in survived #> Explanatory variables: pclass, sex, age #> Prediction dataset : titanic #> Rows shown : 10 of 1,043 #> #> pclass sex age Yes No #> 1st female 29.000 0.877 0.123 #> 1st male 0.917 0.510 0.490 #> 1st female 2.000 0.922 0.078 #> 1st male 30.000 0.376 0.624 #> 1st female 25.000 0.881 0.119 #> 1st male 48.000 0.372 0.628 #> 1st female 63.000 0.891 0.109 #> 1st male 39.000 0.367 0.633 #> 1st female 53.000 0.878 0.122 #> 1st male 71.000 0.451 0.549
predict(result, pred_data = titanic, pred_names = c("Yes", "No"))
#> Naive Bayes Classifier #> Data : titanic #> Response variable : survived #> Level(s) : Yes, No in survived #> Explanatory variables: pclass, sex, age #> Prediction dataset : titanic #> Rows shown : 10 of 1,043 #> #> pclass sex age Yes No #> 1st female 29.000 0.877 0.123 #> 1st male 0.917 0.510 0.490 #> 1st female 2.000 0.922 0.078 #> 1st male 30.000 0.376 0.624 #> 1st female 25.000 0.881 0.119 #> 1st male 48.000 0.372 0.628 #> 1st female 63.000 0.891 0.109 #> 1st male 39.000 0.367 0.633 #> 1st female 53.000 0.878 0.122 #> 1st male 71.000 0.451 0.549
predict(result, pred_cmd = "pclass = levels(pclass)")
#> Naive Bayes Classifier #> Data : titanic #> Response variable : survived #> Level(s) : Yes, No in survived #> Explanatory variables: pclass, sex, age #> Prediction command : pclass = levels(pclass) #> #> age sex pclass Yes No #> 29.813 male 1st 0.377 0.623 #> 29.813 male 2nd 0.215 0.785 #> 29.813 male 3rd 0.110 0.890
result <- nb(titanic, "pclass", c("survived", "sex", "age")) predict(result, pred_data = titanic)
#> Naive Bayes Classifier #> Data : titanic #> Response variable : pclass #> Level(s) : 1st, 2nd, 3rd in pclass #> Explanatory variables: survived, sex, age #> Prediction dataset : titanic #> Rows shown : 10 of 1,043 #> #> survived sex age 1st 2nd 3rd #> Yes female 29.000 0.410 0.303 0.287 #> Yes male 0.917 0.117 0.315 0.568 #> No female 2.000 0.058 0.253 0.689 #> No male 30.000 0.107 0.222 0.672 #> No female 25.000 0.133 0.258 0.609 #> Yes male 48.000 0.633 0.236 0.130 #> Yes female 63.000 0.891 0.098 0.011 #> No male 39.000 0.196 0.262 0.542 #> Yes female 53.000 0.792 0.165 0.043 #> No male 71.000 0.821 0.153 0.026
predict(result, pred_data = titanic, pred_names = c("1st", "2nd", "3rd"))
#> Naive Bayes Classifier #> Data : titanic #> Response variable : pclass #> Level(s) : 1st, 2nd, 3rd in pclass #> Explanatory variables: survived, sex, age #> Prediction dataset : titanic #> Rows shown : 10 of 1,043 #> #> survived sex age 1st 2nd 3rd #> Yes female 29.000 0.410 0.303 0.287 #> Yes male 0.917 0.117 0.315 0.568 #> No female 2.000 0.058 0.253 0.689 #> No male 30.000 0.107 0.222 0.672 #> No female 25.000 0.133 0.258 0.609 #> Yes male 48.000 0.633 0.236 0.130 #> Yes female 63.000 0.891 0.098 0.011 #> No male 39.000 0.196 0.262 0.542 #> Yes female 53.000 0.792 0.165 0.043 #> No male 71.000 0.821 0.153 0.026
predict(result, pred_data = titanic, pred_names = "")
#> Naive Bayes Classifier #> Data : titanic #> Response variable : pclass #> Level(s) : 1st, 2nd, 3rd in pclass #> Explanatory variables: survived, sex, age #> Prediction dataset : titanic #> Rows shown : 10 of 1,043 #> #> survived sex age 1st 2nd 3rd #> Yes female 29.000 0.410 0.303 0.287 #> Yes male 0.917 0.117 0.315 0.568 #> No female 2.000 0.058 0.253 0.689 #> No male 30.000 0.107 0.222 0.672 #> No female 25.000 0.133 0.258 0.609 #> Yes male 48.000 0.633 0.236 0.130 #> Yes female 63.000 0.891 0.098 0.011 #> No male 39.000 0.196 0.262 0.542 #> Yes female 53.000 0.792 0.165 0.043 #> No male 71.000 0.821 0.153 0.026