JaredLanderの教科書「RforEveryone」(第19章)に続いて、ElasticNetを使おうとしています。教科書では、次の「foreach」コードを使用して、並列コーディングを使用して最適なパラメーター値を見つけます。ただし、まったく同じコードを記述して実行したとしても、結果のオブジェクト「acsDouble」は、cv.glmnetオブジェクトの11個のインスタンスを含むリストではありません。代わりに、それは空白のリストです。
教科書のコードを確認し、実行前に環境をクリアしましたが、問題は修正されていません。ここで何が問題になっているようですか?
acs <- read.table("http://jaredlander.com/data/acs_ny.csv", sep=",",
header=TRUE, stringsAsFactors = FALSE)
require(useful)
# make a binary Income variable for building a logistic regression
acs$Income <- with(acs, FamilyIncome >= 150000)
# build predictor matrix
# do not include the intercept as glmnet will add that automatically
acsX <- build.x(Income ~ NumBedrooms + NumChildren + NumPeople +
NumRooms + NumUnits + NumVehicles + NumWorkers +
OwnRent + YearBuilt + ElectricBill + FoodStamp +
HeatingFuel + Insurance + Language - 1,
data=acs, contrasts = FALSE)
# build response predictor
acsY <- build.y(Income ~ NumBedrooms + NumChildren + NumPeople +
NumRooms + NumUnits + NumVehicles + NumWorkers +
OwnRent + YearBuilt + ElectricBill + FoodStamp +
HeatingFuel + Insurance + Language - 1, data=acs)
require(glmnet)
require(parallel)
require(doParallel)
# set the seed for repeatability of random results
set.seed(2834673)
# create folds, we want observations to be in the same fold each time
# it is run
theFolds <- sample(rep(x = 1:5, length.out = nrow(acsX)))
# make sequence of alpha values
alphas <- seq(from = 0.5, to = 1, by = 0.05)
# set the seed for the repeatbility of random results
set.seed(5127151)
# start a cluster with two workers
cl <- makeCluster(2)
# regiser the workers
registerDoParallel(cl)
# keep track of timing
before <- Sys.time()
# build foreach loop to run in parallel
## several arguments
acsDouble <- foreach(i=1:length(alphas), .errorhandling = "remove",
.inorder = FALSE, .multicombine = TRUE,
.export = c("acsX", "acsY", "alphas", "theFolds"),
.packages = "glmnet") %dopar%
{
print(alphas[i])
cv.glmnet(x=acsX, y=acsY, family="binamial", nfolds=5,
foldid = theFolds, alpha = alphas[i])
}
# stop timing
after <- Sys.time()
# make sure to stop the cluster when done
stopCluster(cl)
# time difference
# this will depend on speed, memory & number of cores of the machine
after - before
cv.glmnet
通話にタイプミスがあります。する必要がありfamily="binomial"
ます; 二項ではありません。
.verbose=TRUE
foreachループに含めると、クラスターノードからエラーが発生したかどうかが表示されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加