5

並列化された for ループをプログラムしようとしています。その内部では、テニスをするかどうかを確認するために、p 値が最も低い変数のみをモデル化するための最適な GLM を見つけようとしています (バイナリではい/いいえ)。 .

たとえば、気象データ セットを含むテーブル (およびそのデータフレーム) があります。これらのモデルのどれが最も低い 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