0

I have a raster stcak composed of 70 images. I use the linear mixed model to build the relationship between this raster stack time-series data extracted from several pixels and field measurement data. Now I need to do the predicted values ​​of the remaining pixels of this raster time series, please help?

    library(sf)
    library(raster)
    library(ncdf4)
    library(rgdal)
    library(tidyr)
    library(dplyr)
    library(lmerTest)
    library(arm)
    library(MuMIn)
    library(lme4)
    ####data preparing
    setwd("/Volumes/plan/fusionlst")
    fusionlst = stack(list.files(pattern='*.tif'))
    
    # Convert to degrees
    wgs84.p4s <- "+proj=longlat +datum=NAD83 +ellps=GRS80 +no_defs"
    rx <- projectRaster(from=fusionlst, crs=wgs84.p4s,method="ngb")
   
    plot(rx)
    
    #extract point value of lst
    POINTCOOR=read.csv("pointfile.csv")
    coordinates(POINTCOOR)= ~ LONGITUDE+ LATITUDE
    rasValue=raster::extract(rx, POINTCOOR)
    combinePointValue=cbind(POINTCOOR,rasValue)
    write.table(combinePointValue,file="combinedPointValue.csv", append=FALSE, sep= ",", row.names = FALSE, col.names=TRUE)
    
    #select data that corresponding to field observation data time 
    library(tidyverse)
    library(MuMIn)
    
    aa <- read.csv("combinedPointValue_LMM.csv")
    plot(aa$FCO2,aa$LST)
    cor(aa$FCO2,aa$LST)
    
   #Linear mixed model
    mod_2 <- lmer(FCO2 ~ LST + 
                    (1 | month), 
                    data = aa)
    summary(mod_2)
    r.squaredGLMM(mod_2)
    AIC(mod_2)
    AICc(mod_2)
    BIC(mod_2)
    names(mod_2)
    fitted(mod_2)
    plot(aa$FCO2, fitted(mod_2))
    coef(mod_2)
    mod_2$predicted <- mod_2$fitted.values
    
    #predict values. 
    a$prediction <- predict(mod_2)
    ggplot(a, aes(x = prediction, y = FCO2)) + geom_point() +ggtitle("aaa")
    
    
    #result convert to the .csv file.
    write.csv(a,"mod2.csv", row.names = FALSE)
    write.csv(a)
    
    library(r2mlm)
    library(misty)
    r2 <- r2.multilevel(distrib="logit", fixed.params=fixed.p, predictors=cbind(Data$FCO2), random.vars=random.p)
    
     #predicted by raster 
     #make the list of raster
    rasters <-list.files(pattern = '*.tif$')
    #make raster stack
    s <- stack(rasters)
    #make a time variable 
    time <- 1:nlayers(s)
    # study area
    library(maptools)
    myCRS <- s@crs
    
    myExtent <- readOGR("/Users/xrpt/Downloads/studyarea.shp")

    #run the regression
    fun <- function(x){if (is.na(x[1])){c (NA,NA)} else {lmer(FCO2 ~ LST + (1 | month), data = aa)$cofficients}}
    x2 <- calc(myExtent, fun)
    p1 <- x[[1]] + x[[2]] * time
     #predict to a raster
    predicted <-predict(studyarea, x2 , progress(text))

there is an error occurred like this: <Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘calc’ for signature ‘"SpatialPolygonsDataFrame", "function"’>

1
  • You need to investigate this yourself a bit more. Which line is throwing the error? What are the objects at that point? How did they get to be what they are? Trace it back through the script, checking each line to make sure you get sensible results at each point. Basic fault-finding.
    – Spacedman
    Jun 24 at 5:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.