我正在尝试使用线性模型来预测一些栅格。
这是我的模特:
model1 <- lm(y~x)
model2 <- lm(y~x+c)
c是一个国家/地区因素,每个国家/地区代码一个级别。我想使用predict
函数,其中每个国家/地区的新x值都放在一个栅格中。
library(raster)
ntl <- readRaster("ntl_BRA.tif")
ctry <- "BRA"
p1 = predict(model1, newdata=data.frame(x=ntl[]))
p2 = predict(model2, newdata=data.frame(x=ntl[],c=ctry))
预测工作较慢,但对于第一个模型(没有因素)很好。但是,在模型中包含一个因素时,出现以下错误:
无法分配大小为31.9 Gb的向量
每个国家/栅格层仅使用一个因素,因此对我来说似乎并不需要那么多的额外内存。
我错误地定义了因素吗?
以下是数据的详细信息:
> ntl
> class : RasterLayer
> dimensions : 4347, 4957, 21548079 (nrow, ncol, ncell)
> resolution : 0.004166667, 0.004166667 (x, y)
> extent : -8.668749, 11.98542, 18.97708, 37.08958 (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
> data source : in memory
> names : layer
> values : -4.60517, 9.519959 (min, max)
和我从中创建模型的数据:
> str(rdata)
'data.frame': 1659 obs. of 3 variables:
$ x: num 9.39 9.12 8.53 10.83 8.03 ...
$ y: num 8.81 8.77 9.25 9.24 9.17 ...
$ c: Factor w/ 198 levels "ABW","AFG","AGO",..: 190 190 147 147 147 147 147 147 147 147 ...