5

我正在尝试编写一个并行化的 for 循环,在其中我试图以最佳方式找到最佳 GLM 以仅对具有最低 p 值的变量进行建模,以查看我是否要打网球(二进制是/否) .

例如,我有一个包含气象数据集的表(及其数据框)。我通过首先查看其中哪个模型的 p 值最低来构建 GLM 模型

PlayTennis ~ Precip
PlayTennis ~ Temp, 
PlayTennis ~ Relative_Humidity
PlayTennis ~ WindSpeed)

假设PlayTennis ~ Precip具有最低的 p 值。因此,repeat 中的下一个循环迭代是查看其他变量的 p 值最低。

PlayTennis ~ Precip + Temp
PlayTennis ~ Precip + Relative_Humidity 
PlayTennis ~ Precip + WindSpeed

这将一直持续到没有更重要的变量(P 值大于 0.05)。因此,我们得到了PlayTennis ~ Precip + WindSpeed(这都是假设的)的最终输出。

关于如何在各种内核上并行化此代码有什么建议吗?我遇到了一个speedglm从库 speedglm 调用的 glm 新函数。这确实有所改善,但幅度不大。我也研究了foreach循环,但我不确定它如何与每个线程进行通信,以了解在各种运行中哪个 p 值更大或更小。预先感谢您的任何帮助。

d =

Time          Precip    Temp    Relative_Humidity   WindSpeed   …   PlayTennis    
1/1/2000 0:00   0        88           30                0              1    
1/1/2000 1:00   0        80           30                1              1    
1/1/2000 2:00   0        70           44                0              1    
1/1/2000 3:00   0        75           49               10              0    
1/1/2000 4:00   0.78     64           99               15              0    
1/1/2000 5:00   0.01     66           97               15              0    
1/1/2000 6:00   0        74           88                8              0    
1/1/2000 7:00   0        77           82                1              1    
1/1/2000 8:00   0        78           70                1              1    
1/1/2000 9:00   0        79           71                1              1

我拥有的代码如下:

newNames <- names(d)
FRM <- "PlayTennis ~" 

repeat
{
    for (i in 1:length(newNames))
    {
        frm <- as.formula(paste(FRM, newNames[i], sep =""))
        GLM <- glm(formula = frm, na.action = na.exclude, # exclude NA values where they exist
                    data = d, family = binomial())
        # GLM <- speedglm(formula = frm, na.action = na.exclude, # exclude NA values where they exist
        #                 data = d, family = binomial())

        temp <- coef(summary(GLM))[,4][counter]

        if (i == 1) # assign min p value, location, and variable name to the first iteration
        {
            MIN <- temp
            LOC <- i
            VAR <- newNames[i]
        }

        if (temp < MIN) # adjust the min p value accordingly
        {
            MIN <- temp
            LOC <- i
            VAR <- newNames[i]
        }
    }

    if(MIN > 0.05) # break out of the repeat loop when the p-value > 0.05
    {
        break
    }

    FRM <- paste(FRM, VAR, " + ", sep = "") # create new formula
    newNames <- newNames[which(newNames != VAR)] # removes variable that is the most significant
    counter <- counter + 1
}

我尝试过但不起作用的代码

newNames <- names(d)
FRM <- "PlayTennis ~" 

repeat
{
    foreach (i = 1:length(newNames)) %dopar%
    {
        frm <- as.formula(paste(FRM, newNames[i], sep =""))
        GLM <- glm(formula = frm, na.action = na.exclude, # exclude NA values where they exist
                    data = d, family = binomial())
        # GLM <- speedglm(formula = frm, na.action = na.exclude, # exclude NA values where they exist
        #                 data = d, family = binomial())

        temp <- coef(summary(GLM))[,4][counter]

        if (i == 1) # assign min p value, location, and variable name to the first iteration
        {
            MIN <- temp
            LOC <- i
            VAR <- newNames[i]
        }

        if (temp < MIN) # adjust the min p value accordingly
        {
            MIN <- temp
            LOC <- i
            VAR <- newNames[i]
        }
    }

    if(MIN > 0.05) # break out of the repeat loop when the p-value > 0.05
    {
        break
    }

    FRM <- paste(FRM, VAR, " + ", sep = "") # create new formula
    newNames <- newNames[which(newNames != VAR)] # removes variable that is the most significant
    counter <- counter + 1
} 
4

0 回答 0