botinky
botinky

Reputation: 27

Comparing R-Squared, AIC and BIC results for multiple models for multiple datasets in R loop

I've got a dataframe with R-squared, AIC and BIC results for multiple models (originally 6 with NAs (failed models) removed ) for multiple datasets (ID 1-285). It looks something like this:

R.Squared   AIC.Score   BIC.Score   ID  Model
0.995974109 -72.3098650 -65.6488424 1   Gompertz
0.934979868 3.5847545   8.9135726   1   Quadratic
0.987590443 -40.7896873 -34.1286648 1   Polynomial
0.714849861 42.9776837  46.9742972  1   Linear
0.997938552 -129.21663  -122.555616 2   Gompertz
0.606529548 15.8280808  21.1568988  2   Quadratic
0.836863960 -6.8237218  -0.1626993  2   Polynomial
0.278205570 30.8166440  34.8132576  2   Linear
0.996508677 -75.9122185 -69.2511959 3   Gompertz
0.994680122 -64.1194699 -57.4584473 3   Baranyi
0.622752101 53.2011917  58.5300097  3   Quadratic
0.855506577 28.3304727  34.9914952  3   Polynomial
0.260179452 70.0593352  74.0559487  3   Linear
0.939319678 8.7821245   15.7881114  4   Gompertz
0.946040756 5.2604112   12.2663981  4   Baranyi
0.786132773 44.5742023  50.1789919  4   Quadratic
0.943966980 6.3917756   13.3977625  4   Polynomial
0.213360142 81.6466566  85.8502487  4   Linear

This is the code I've been trying to use to extract the model name for best score for each ID:


for (i in 1:X) { 

 ID_stats <- total_stats_results[which(total_stats_results$ID==i),]

 #SAVE BEST MODEL DATA TO DATAFRAME

 R.Squared <- max(ID_stats[,"R.Squared"])
 best_model_R <- ID_stats[ID_stats$R.Squared == R.Squared, "Model"]
 best_model_R <- as.character(best_model_R)

 AIC <- min(abs((ID_stats[,"AIC.Score"])))
 best_model_AIC <- ID_stats[ID_stats$AIC.Score == AIC, "Model"]
 best_model_AIC <- as.character(best_model_AIC)

 BIC <- min(abs((ID_stats[,"BIC.Score"])))
 best_model_BIC <- ID_stats[ID_stats$BIC.Score == BIC, "Model"]
 best_model_BIC <- as.character(best_model_BIC)

 final_stats <- data.frame(i, best_model_R, best_model_AIC, best_model_BIC)
 names(final_stats) <- c("ID", "Best R-Sq Model", "Best AIC Model", "Best BIC Model")

 write.csv(final_stats, paste0("../results/model_fits/final_stats_",i,".csv"))

}

But everytime I try and run my loop I get this error:

Error in data.frame(i, best_model_R, best_model_AIC, best_model_BIC) : 
  arguments imply differing number of rows: 1, 0

Any help would be much appreciated! Cheers x

Upvotes: 0

Views: 259

Answers (1)

SmokeyShakers
SmokeyShakers

Reputation: 3412

In each step like:

AIC <- min(abs((ID_stats[,"AIC.Score"])))

You take the absolute value. But in the next step you use the absolute value to subset the original data.

 best_model_AIC <- ID_stats[ID_stats$AIC.Score == AIC, "Model"]

The absolute value might not appear in the original data.

Upvotes: 1

Related Questions