0

我有一个名为rain的 SpatialPointsDataframe ,我想为其最后 10 列(因变量)中的每一列拟合一个变异函数并执行交叉验证,如下所示:

  fit.reg.vgm <- autofitVariogram(
  column (dependent variable) ~ X + Y + Z + AS + SL,
  rain,
  model = c("Sph", "Exp", "Gau", "Lin", "Log"),
  fix.values = c(NA, NA, NA),
  verbose = FALSE,
  GLS.model = NA,
  start_vals = c(NA, NA, NA),
  miscFitOptions = list()
)
  cv <-krige.cv(column (dependent variable) ~ X + Y + Z + AS + SL, rain, fit.reg.vgm$var_model)

有谁知道如何构建这样的for循环?

提前致谢!

4

2 回答 2

0

您将需要构建一个公式。尝试formula()paste()。类似的东西

x <- c("a", "b", "c")
out <- list()

for (i in seq_along(x)) {
  out[[i]] <- formula(paste(x[i], "~ X + Y + Z"))
}


> out
[[1]]
a ~ X + Y + Z

[[2]]
b ~ X + Y + Z

[[3]]
c ~ X + Y + Z
于 2020-10-06T20:12:20.533 回答
0

一个选项reformulate

out <- vector('list', length(x))
for(i in seq_along(x)) {out[[i]] <- reformulate(c("X", "Y", "Z"), response = x[i]) }
out
#[[1]]
#a ~ X + Y + Z

#[[2]]
#b ~ X + Y + Z

#[[3]]
#c ~ X + Y + Z
于 2020-10-06T22:07:53.457 回答