R で順方向ステップワイズ回帰を実装したいと考えています。BFP は BodyFatPercentage データ セットであり、ステップワイズ回帰を通じて BODYFAT を予測する回帰モデルを作成しようとしています。エラーが発生し続ける
(エラー: ブレーク/次のループがありません。トップ レベルにジャンプします
}}
エラー: " }" に予期しない '}' があります
次のコードを使用します。
dataset <- BFP
alpha <- 0.01
namestarget <- 'BODYFAT'
inde <- c('AGE','WEIGHT','HEIGHT','NECK','CHEST','ABDOMEN',
'HIP','THIGH','KNEE','ANKLE','BICEPS','FOREARM','WRIST')
x <- 1
counter <- 0
indiLeft <- as.data.frame(subset(dataset)[inde])
fmla_sub <- NULL
fmla_sup <- NULL
while (TRUE){
print c('starting',counter,newx, indiLeft)
correlations <- cor(dataset[target],indiLeft)
newx <- colnames(correlations)[(which(correlations == max(correlations)))]
fmla_sub <- as.formula(paste(target,"~", paste(x, collapse= "+")))
fmla_sup <- as.formula(paste(target,"~", paste(c(x,newx), collapse= "+")))
p <- anova(lm(fmla_sub,data=dataset),lm(fmla_sup,data=dataset), test="F")['Pr(>F)']
if (p$'Pr(>F)'[2] < alpha){
x<- c(x,newx)
indiLeft <- indiLeft[-which(names(indiLeft) == newx)]
counter <- counter +1
next
}else{
print ('while broken')
print (fmla_sub)
break
}
}
このwhileループがループを1回しか試行しない理由を誰でも理解できますか?