[R-sig-Geo] Help with spTimer or spTDyn to estimate the Bayesian Gaussian Process Model
@iex_rugu m@iii@g oii y@hoo@com
@iex_rugu m@iii@g oii y@hoo@com
Wed Feb 3 21:42:45 CET 2021
I am getting the following error when running the below script for the Bayesian Gaussian Process (GP) Model
using spTimer or spTDyn
#Error
Output: GP models
Error in spGP.Gibbs(formula = formula, data = data, time.data = time.data, :
Error: Years, Months, and Days are misspecified,
i.e., total number of observations in the data set should be equal to N
: N = n * r * T
where, N = total number of observations in the data,
n = total number of sites,
r = total number of years,
T = total number of days.
#######Script
library(spTimer)
library(spTDyn)
library(tidyverse)
library(ggmap)
register_google(key="your key")
getOption("ggmap")
#Data to be analyzed is from plm package
#It is a US States Production, which is a panel of 48 observations from 1970 to 1986
#A data frame containing :
#state: the state
#year : the year
#region : the region
#pcap : public capital stock
#hwy : highway and streets
#water : water and sewer facilities
#util : other public buildings and structures
#pc : private capital stock
#gsp : gross state product
#emp :labor input measured by the employment in non–agricultural payrolls
#unemp : state unemployment rates
#Get data
data("Produc", package = "plm")
glimpse(Produc)
#Estimate Geolocation of states to account for spill over effects
states_df <- data.frame(as.character(unique(Produc$state)))
names(states_df)<- c("state")
state_geo_df <- mutate_geocode(states_df, state) #from ggmap
#Join the data
Product_geo <- full_join(state_geo_df, Produc)
glimpse(Product_geo)
#Create the time series variable
#number of state
ns <- length(unique(Product_geo$state))
#number of year
ny <- length(unique(Product_geo$year))
####################################################
# I want to do Spatio-Temporal Bayesian Modeling Using spTimer or spTDyn
#defines the time series in the Spatio-temporal framework
ts_STD <- def.time(t.series=ns, segments=ny)
##################Estimate the model using spTDyn package
#Note spT.Gibbs in spTimer gives the same error
GibbsDyn(gsp ~ pcap + hwy + water + util + pc ,
data=Product_geo, model="GP",
time.data=ts_STD,
coords=~lon + lat,
nItr=5000, nBurn=1000, report=1, tol.dist=0.05,
distance.method="geodetic:km", cov.fnc="exponential",
spatial.decay=decay(distribution="FIXED"),truncation.para=list(at=0,lambda=2))
#Also how to deal with unbalanced panel
#Delete some of the rows
Product_geo$cond = with(Product_geo, if_else(state=="ALABAMA" & year==1971, 0,
if_else(state=="COLORADO" & year==1971 | year==1973 , 0,
if_else(state=="TEXAS" & year==1971 | year==1973 | year==1985, 0, 1))))
Product_geo_unb <- Product_geo %>% filter(cond==1) %>% select(-cond)
glimpse(Product_geo_unb)
#How to use GibbsDyn or spT.Gibbs for such unbalanced panel data to estimate the Bayesian Gaussian Process (GP) Model?
More information about the R-sig-Geo
mailing list