################################# NANP Nutrition Models Workshop ################################# # Automated model selection (AMS): Part II (exercises) # Dr. Veridiana L. Daley Email: veridi7@vt.edu # Script notes: # R code to evaluate models from past NANP Workshops (https://animalnutrition.org/workshops-symposia). ################################ Additional Lesson ############################################ # Show the variable names of the dataset names(data) # Identify studies (PubIDS) with missing BW data is.na(data$An_BW) # # returns a vectorFalse or True (F F F T) # Add new variables to a data frame data$MilkFatYield <- data$Obs_MilkProd * data$Obs_MilkFatp/100 # calculate milk fat yield (kg/d) # Check character class of all variables of the dataset library(tidyverse) map_chr(data, class) # Plot: Frequency of Studies (PubIDs) (run the code < go to Plots < Zoom) colors = c("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan") PubID <- d$PubID PubID.freq <- table(PubID) # apply the table function barplot(PubID.freq, col=colors) # apply the barplot function # Function to change digits round_df <- function(df, digits) { nums <- vapply(df, is.numeric, FUN.VALUE = logical(1)) df[,nums] <- round(df[,nums], digits = digits) (df) } # Descriptive statistics library(psych) Stat <- as.data.frame(describe(d)) Stat <- round_df(Stat, digits=2) # call the function to change the digits (round_df(datasetname, digits=2)) # Export results for descriptive statistics (it will be saved in the working directory (folder) write.csv(Stat, "Descriptive_Statistic.csv")