Data.S2. R script for comparision of panicle weight prediction accuracy between linear regression and machine learning model using MAE (mean absolute error). The data of 100 rice varieties were split into model training data set (80 varieties) and model test data set (20 varieties) using sample function in R. A sequential neural network model is created using the Keras library. The four parameters ("HEIGHT_PX_side","Plant_Height","RGB.57.71.46.side","PRI.stddev") for model training is determined using subset selection regression.

# load libraries for model evaluation

library(keras)
library(caret) 
library(readr)

# Load the data, make sure to change the dir of your data file, hear I store the data in the "a115ok.csv" file

panicle_weight <- read.csv("a115ok.csv") 
panicle_weight <- na.omit(panicle_weight) #remove any rows with missing values

# Use four parameters ("HEIGHT_PX_side","Plant_Height","RGB.57.71.46.side","PRI.stddev") for training, this four parameters are identified using Subset Selection Regression, see Table S2.

panicle_weight <- panicle_weight[, c("HEIGHT_PX_side","Plant_Height","RGB.57.71.46.side","PRI.stddev" ,"panicle_weight")]

# the data of 100 rice varieties are split into a training set (80%) and a test set (20%) using the “sample()” function

ind <- sample(2, nrow(panicle_weight), replace = T, prob = c(0.8, 0.2)) 

training <- panicle_weight[ind == 1, 1:4]
test <- panicle_weight[ind == 2, 1:4]
trainingtarget <- panicle_weight[ind == 1, 5]
testtarget <- panicle_weight[ind == 2, 5]

# A linear regression model is created using the “lm()” function, with the "panicle_weight" as the target variable and the four selected parameters as the predictors.

lm_model <- lm(trainingtarget ~ ., data = training)

# Evaluate the linear regression model

lm_predictions <- predict(lm_model, newdata = test)
lm_mae <- mean(abs(lm_predictions - testtarget))
cat("Linear regression model MAE:", lm_mae, "\n")

# Evaluate the machine learning model

m <- colMeans(training)
s <- apply(training, 2, sd)
training <- scale(training, center = m, scale = s)
test <- scale(test, center = m, scale = s)

model <- keras_model_sequential() %>% 
    layer_dense(units = 10, activation = 'relu', input_shape = c(4)) %>%
    layer_dense(units = 1)

model %>% compile(loss = 'mse', 
                  optimizer = 'rmsprop', 
                  metrics = 'mae')

mymodel <- model %>%
    fit(training,
        trainingtarget,
        epochs = 500,
        batch_size = 32,
        validation_split = 0.2)

ml_predictions <- model %>% predict(test)
ml_mae <- mean(abs(ml_predictions - testtarget))
cat("Machine learning model MAE:", ml_mae, "\n")

# Compare the performance
cat("Linear regression model MAE:", lm_mae, "\n")
cat("Machine learning model MAE:", ml_mae, "\n")
