0

我是 R 的新手。现在我正在处理 R 包 GWmodel。

查看基本 GWR 的函数,如下所示:

gwr.res <- gwr.basic(GenEl2004 ~ DiffAdd + LARent + SC1 + Unempl + LowEduc + Age18_24 + Age25_44 + Age45_64, data = Dub.voter, bw = 100,  kernel = "bisquare", adaptive = TRUE, F123.test = TRUE)

我需要收集每个变量的估计参数的平均值,并将其附加到任何给定 bw 值(带宽)的列表中。

在 python 术语中,这将是这样的:

LARentMean = []
SC1Mean = []
UnenmplMean = []
LowEducMean = []
Age18_24Mean = []
Age25_44Mean = []
Age45_64Mean = []


for i in range (20,400):
    gwrres = gwr.basic(GenEl2004 ~ DiffAdd + LARent + SC1 + Unempl + LowEduc + Age18_24 + Age25_44 + Age45_64, data = Dub.voter, bw = i,  kernel = "bisquare", adaptive = TRUE, F123.test = TRUE)
    a = gwrres(LARent).mean()    #a <- mean(gwrres$SDF$LARent)
    b = gwrres(SC1).mean()       #b <- mean(gwrres$SDF$SC1)
    c = gwrres(Unenmpl).mean()   #c <- mean(gwrres$SDF$Unempl)
    d = gwrres(lowEduc).mean()   #d <- mean(gwrres$SDF$LowEduc)
    e = gwrres(Age18_24).mean()  #e <- mean(gwrres$SDF$Age18_24)
    f = gwrres(Age25_44).mean()  #f <- mean(gwrres$SDF$Age25_44)
    g = gwrres(Age45_64).mean()  #g <- mean(gwrres$SDF$Age45_64)
    LARentMean.append(a)
    SC1Mean.append(b)
    UnenmplMean.append(c)
    LowEducMean.append(d)
    Age18_24Mean.append(e)
    Age25_44Mean.append(f)
    Age45_64Mean.append(g)
4

1 回答 1

0

您可以使用 lapply 它将循环到一个列表,例如:

l = lapply(20:400, function(i){
    gwr.basic(GenEl2004 ~ DiffAdd + LARent + SC1 + Unempl + LowEduc + 
        Age18_24 + Age25_44 + Age45_64, data=Dub.voter, bw=i,
        kernel="bisquare", adaptive=T, F123.test=T)
})

我不知道 gwr.basic 生成什么作为输出,因此您可能需要额外的一行来仅取平均值(如果它提供更多信息)。

于 2015-08-22T11:17:22.960 回答