ahmet varlı
2020-Sep-09 14:13 UTC
[R] calculating a linear regression for each grid cell. firstly
Hi all, for example, ? have 4 raster data and ? try to convert these rasters to 1 array and calculate a linear regression for each grid cell how can ? do this? max_consecutive_days_1<- raster(ncol=94, nrow=192, xmn=-180, xmx=180, ymn=-90, ymx=90) max_consecutive_days_2<- raster(ncol=94, nrow=192, xmn=-180, xmx=180, ymn=-90, ymx=90) max_consecutive_days_3<- raster(ncol=94, nrow=192, xmn=-180, xmx=180, ymn=-90, ymx=90) max_consecutive_days_4<- raster(ncol=94, nrow=192, xmn=-180, xmx=180, ymn=-90, ymx=90) values(max_consecutive_days_1) <- 1:ncell(max_consecutive_days_1) values(max_consecutive_days_2) <- 1:ncell(max_consecutive_days_2) values(max_consecutive_days_3) <- 1:ncell(max_consecutive_days_3) values(max_consecutive_days_4) <- 1:ncell(max_consecutive_days_4) set.seed(0) values(max_consecutive_days_1) <- runif(ncell(max_consecutive_days_1)) values(max_consecutive_days_2) <- runif(ncell(max_consecutive_days_2)) values(max_consecutive_days_3) <- runif(ncell(max_consecutive_days_3)) values(max_consecutive_days_4) <- runif(ncell(max_consecutive_days_4)) I tried to convert raster to an array and calculate a linear regression for each cell with these codes. there is something wrong in codes but I didn't find what it's wrong library(raster) r<-raster("C:/max_consecutive_days_1.tif") a<-array(NA,dim=c(dim(r)[1:2],4)) i <- 1 dir.in <- "C:/max_consecutive_days/" for (year in 1:4) { fi<-paste0(dir.in,"max_consecutive_days_",year,".tif") r<-raster(fi) a[,,i]<-getValues(r,format="matrix") i<-i+1 } lmfunction <- function(x){ if (is.na(x[1])){ NA } else y<-c(1:length(x)) m1<- summary(lm(x~y)) coef <- m1$coef[2] coef } storem <- array(NA,dim=dim(a)[1:2]) for(i in 1:94){ for(j in 1:192){ tst<-mean(a[i,j,],n.rm=T) if (is.na(tst)==F){ storem[i,j]<-lmfunction(a[i,j,]) } } } Best wishes, Windows 10 i?in Posta<https://go.microsoft.com/fwlink/?LinkId=550986> ile g?nderildi [[alternative HTML version deleted]]