R和gstat.predict出错

时间:2015-02-14 13:55:35

标签: r gstat

我正在尝试使用R中的gstat包来添加协变量进行一些预测。

我按以下方式生成gstat对象:

# We use the prediction sample for pm2.5 (primary) and pm10 (secondary) variable
g <- gstat(NULL, "ln.pm2.5", log(pm25.obs) ~ log(pd$pm25.LE), pd)

# Append the other variables to g
g <- gstat(g, "ln.pm10", log(pm10.obs) ~ log(ndpm10$pm10.LE), ndpm10)

v <- variogram(g)
# Check the output and plot the result
v
plot(v)
# Define the initial variogram model and append it to g
g <- gstat(g, model=vgm(0.04, "Sph", 600, 0.1), fill.all=TRUE)
#g <- gstat(g, model=vgm(0.10, "Sph", 200, 0.03), fill.all=TRUE)# for region B
# Use the LMC for fitting
g.fit <- fit.lmc(v, g)
g.fit
plot(v, g.fit)

所以g对象看起来像这样:

> g
data:
ln.pm2.5 : formula = log(pm25.obs)`~`log(pd$pm25.LE) ; data dim = 218 x 10
ln.pm10 : formula = log(pm10.obs)`~`log(ndpm10$pm10.LE) ; data dim = 958 x 10
variograms:
                    model      psill range
ln.pm2.5[1]           Nug 0.06798841     0
ln.pm2.5[2]           Sph 0.10237623   600
ln.pm10[1]            Nug 0.10325992     0
ln.pm10[2]            Sph 0.06879960   600
ln.pm2.5.ln.pm10[1]   Nug 0.08378829     0
ln.pm2.5.ln.pm10[2]   Sph 0.08392523   600

如果我尝试使用此g对象进行预测,则会出现以下错误:

> > tmp1 <- predict.gstat(g, newdata=R1.ctm.data) Error in gstat.formula.predict(d$formula, newdata, na.action = na.action,  :   
> NROW(locs) != NROW(X): this should not occur In addition: Warning
> messages: 1: 'newdata' had 2220 rows but variables found have 218 rows
> 2: 'newdata' had 2220 rows but variables found have 218 rows

其中R1.ctm.data具有以下结构:2220观察SpatialPoints DataFrame,其具有北向,东向以及pm2.5和pm10(协变量)信息。如果我删除协变量,我没有问题。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

由于我无法重现这一点,我只能猜测。可能是您的协变量包含缺失的(NA)值;请确保在创建gstat对象之前以及在调用predict之前删除响应和协变量中缺少值的记录。