0

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?

MGD
  • 1
  • 6
  • what do you struggle with ? loading a csv? loading many csvs? taking mean values ? Considering the help you require, what is it your priority to learn about?... what is your sticking point ? – Nir Graham Jun 13 '23 at 09:12
  • I can load mean values in R but I have no idea about how to use those mean values to raster file to predict model. – MGD Jun 13 '23 at 09:22
  • I would break down your problem into two steps. 1) Reconstructing a `pls` style model from the mere coefficients recorded. ( I think pls supports different model varieties so you may need to be more specific about what you did ) . and secondly 2) apply a pls sytle model to a raster. If I was you I would work backwards, as there would seem little point solving 1) if 2) is a dead end. You should research how models are applied to rasters in general, and maybe try to do so with a relatively trivial example to prove the route. – Nir Graham Jun 13 '23 at 09:52
  • Example added.. – MGD Jun 13 '23 at 22:13

0 Answers0