How can external coefficient values (saved in CSV) to be used to predict model in R?

48 Views Asked by At

I have data containing 100 obs, with 50 X-variables and 1 Y-variable. For 100 permutation, I have randomly divided data in 60-40 for training and testing respectively and run a pls (from pls package in R) model for 100 times.. so that I have 100 models with 100 coefficient values (incl. intercept) for each variables, which are saved in CSV file. I need to apply mean model of those 100 models to a raster image file.. How can I do this in R?

library(pls)
library(raster)

# loading data
my_data <- read.csv("file1.csv", header = TRUE)

# 100 times permutation
for (i in 1:100) {
  index = sample(1:nrow(my_data), size = 0.6*nrow(my_data))
  cal_data = my_data[index,]
  val_data = my_data[-index,]

  # running pls model for 100 times
  pls_model <- plsr(Y~., data = cal_data, ncomp = 10, validation = "LOO")

  # estimate validation data
  Y.val <- val_data$Y
  pred.val_data = as.vector(predict(pls_model, newdata = val_data[,2:51], ncomp = 10)[,,1])
  output.coefs[i,] <- as.vector(coef(pls_model, ncomp = 10, intercept = TRUE))
}

# load raster image
my_raster <- raster("example_raster1")

I want to apply mean coefficient values of "output.coefs" on my_raster file.

How can I do this in R?

0

There are 0 best solutions below