call the terra package

library("terra")
## terra 1.7.78

raster with 100 cells

r <- rast(ncol=10, nrow=10)
ncell(r)
## [1] 100
hasValues(r)
## [1] FALSE
# use the 'values' function, e.g.,
values(r) <- 1:ncell(r)
# or
set.seed(0)
values(r) <- runif(ncell(r))
hasValues(r)
## [1] TRUE
sources(r)
## [1] ""
values(r)[1:10]
##  [1] 0.8966972 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819 0.8983897
##  [8] 0.9446753 0.6607978 0.6291140
##  [1] 0.8966972 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819 0.8983897
##  [8] 0.9446753 0.6607978 0.6291140
plot(r, main='Raster with 100 cells')

raster map from file

filename <- system.file("ex/meuse.tif", package="terra")
filename
## [1] "C:/Users/154455/AppData/Local/R/win-library/4.4/terra/ex/meuse.tif"
r <- rast(filename)
sources(r)
## [1] "C:/Users/154455/AppData/Local/R/win-library/4.4/terra/ex/meuse.tif"
hasValues(r)
## [1] TRUE
plot(r, main='SpatRaster from file')

data points

p <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85,
   66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31,
   22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
a <- matrix(c(22, 33, 64, 85, 92, 94, 59, 27, 30, 64, 60, 33, 31, 9,
   99, 67, 15, 5, 4, 30, 8, 37, 42, 27, 19, 69, 60, 73, 3, 5, 21,
   37, 52, 70, 74, 9, 13, 4, 17, 47), ncol=2)
xy <- rbind(cbind(1, p), cbind(0, a))
xy
##       [,1] [,2] [,3]
##  [1,]    1   48    7
##  [2,]    1   48   14
##  [3,]    1   48   26
##  [4,]    1   53   29
##  [5,]    1   50   39
##  [6,]    1   46   45
##  [7,]    1   54   51
##  [8,]    1   70   56
##  [9,]    1   84   46
## [10,]    1   85   38
## [11,]    1   74   31
## [12,]    1   84   22
## [13,]    1   95   34
## [14,]    1   85   60
## [15,]    1   66   70
## [16,]    1   42   73
## [17,]    1   26   63
## [18,]    1    4   46
## [19,]    1   19   43
## [20,]    1   17   28
## [21,]    0   22    8
## [22,]    0   33   37
## [23,]    0   64   42
## [24,]    0   85   27
## [25,]    0   92   19
## [26,]    0   94   69
## [27,]    0   59   60
## [28,]    0   27   73
## [29,]    0   30    3
## [30,]    0   64    5
## [31,]    0   60   21
## [32,]    0   33   37
## [33,]    0   31   52
## [34,]    0    9   70
## [35,]    0   99   74
## [36,]    0   67    9
## [37,]    0   15   13
## [38,]    0    5    4
## [39,]    0    4   17
## [40,]    0   30   47
# extract predictor values for points
e <- extract(logo, xy[,2:3])
e
##    red green blue
## 1  194   205  237
## 2  147   161  208
## 3  142   156  203
## 4  221   228  254
## 5  162   173  229
## 6  115   129  174
## 7  195   202  230
## 8  179   187  233
## 9  110   116  152
## 10  34    40   66
## 11 240   248  255
## 12 162   167  171
## 13 189   191  180
## 14 102    99   90
## 15 187   184  177
## 16 196   196  184
## 17 171   171  163
## 18 187   189  176
## 19  13    12   10
## 20 181   181  173
## 21 255   255  255
## 22 255   255  255
## 23 246   249  242
## 24 246   249  242
## 25 253   255  254
## 26 255   255  255
## 27 254   255  250
## 28 247   249  238
## 29 255   255  255
## 30 255   255  253
## 31 245   247  244
## 32 255   255  255
## 33 255   255  255
## 34 253   253  253
## 35 255   255  255
## 36 254   255  255
## 37 255   255  255
## 38 255   255  255
## 39 255   255  255
## 40 255   255  255
# combine with response
v <- data.frame(cbind(pa=xy[,1], e))
v
##    pa red green blue
## 1   1 194   205  237
## 2   1 147   161  208
## 3   1 142   156  203
## 4   1 221   228  254
## 5   1 162   173  229
## 6   1 115   129  174
## 7   1 195   202  230
## 8   1 179   187  233
## 9   1 110   116  152
## 10  1  34    40   66
## 11  1 240   248  255
## 12  1 162   167  171
## 13  1 189   191  180
## 14  1 102    99   90
## 15  1 187   184  177
## 16  1 196   196  184
## 17  1 171   171  163
## 18  1 187   189  176
## 19  1  13    12   10
## 20  1 181   181  173
## 21  0 255   255  255
## 22  0 255   255  255
## 23  0 246   249  242
## 24  0 246   249  242
## 25  0 253   255  254
## 26  0 255   255  255
## 27  0 254   255  250
## 28  0 247   249  238
## 29  0 255   255  255
## 30  0 255   255  253
## 31  0 245   247  244
## 32  0 255   255  255
## 33  0 255   255  255
## 34  0 253   253  253
## 35  0 255   255  255
## 36  0 254   255  255
## 37  0 255   255  255
## 38  0 255   255  255
## 39  0 255   255  255
## 40  0 255   255  255

GLM

#build a model, here with glm
model <- glm(formula=pa~., data=v)
#predict to a raster
r1 <- predict(logo, model)
plot(r1)
points(p, bg='blue', pch=21)
points(a, bg='red', pch=21)

pca

sr <- values(spatSample(logo, 100, as.raster=TRUE))
pca <- prcomp(sr)
x <- predict(logo, pca)
plot(x)

Reference - https://rspatial.org/pkg/9-predict.html