library(sp)
library(spacetime)
library(sf)
library(gstat)
library(plyr)
# prepare data
# load observation - data.frame of mean temperatures
# preparing data
data(dtempc)
data(stations)
data(regdata) # covariates, made by mete2STFDF function
regdata@sp@proj4string <- CRS('+proj=longlat +datum=WGS84')
lonmin=18 ;lonmax=22.5 ; latmin=40 ;latmax=46
serbia = point.in.polygon(stations$lon, stations$lat, c(lonmin,lonmax,lonmax,lonmin),
c(latmin,latmin,latmax,latmax))
st = stations[ serbia!=0, ] # stations in Serbia approx.
obs.staid.time = c("staid", "time")
stations.staid.lon.lat = c(1,2,3)
crs = CRS('+proj=longlat +datum=WGS84')
delta = NULL
# \donttest{
# create STFDF
stfdf <- meteo2STFDF(obs = dtempc,
stations = st,
crs = crs)
# Calculate prediction of mean temperatures for "2011-07-05" and "2011-07-06"
# global model is used for regression and variogram
# load precalculated variograms
data(tvgms) # ST variogram models
data(tregcoef) # MLR coefficients
### Example with STFDF and without parallel processing
results <- pred.strk(data = stfdf, # observations
newdata = regdata, # prediction locations with covariates
# newdata = regdata[,2,drop=FALSE], # for one day only
output.format = "STFDF", # data.frame | sf | sftime | SpatVector | SpatRaster
reg.coef = tregcoef[[1]], # MLR coefficients
vgm.model = tvgms[[1]], # STRK variogram model
sp.nmax = 20,
time.nmax = 2,
computeVar=TRUE
)
class(results)
# plot prediction
results@sp=as(results@sp,'SpatialPixelsDataFrame')
stplot(results[,,"pred", drop= FALSE], col.regions=bpy.colors())
stplot(results[,,"var", drop= FALSE], col.regions=bpy.colors())
# }
# Example with data.frames and parallel processing - SpatRaster output
# \donttest{
library(terra)
library(doParallel)
# create data.frame
stfdf.df <- join(dtempc, st)
summary(stfdf.df)
regdata.df <- as.data.frame(regdata)
results <- pred.strk(data = stfdf.df,
obs.col = 3,
data.staid.x.y.z = c(1,4,5,2),
newdata = regdata.df,
newdata.staid.x.y.z = c(3,1,2,4),
crs = CRS("EPSG:4326"),
output.format = "SpatRaster", # STFDF |data.frame | sf | sftime | SpatVector
reg.coef = tregcoef[[1]],
vgm.model = tvgms[[1]],
sp.nmax = 20,
time.nmax = 2,
parallel.processing = TRUE,
pp.type = "doParallel", # "snowfall"
cpus = 2, # detectCores()-1,
computeVar = TRUE,
progress = TRUE
)
# plot prediction
plot(results$`2011-07-06`[["pred"]])
plot(results$`2011-07-06`[["var"]])
# }
Run the code above in your browser using DataLab