Wie automatisieren Sie die variable Auswahl in glmnet und cross-Validierung

Lerne ich die Verwendung von glmnet und brnn Pakete. Betrachten Sie den folgenden code:

library(RODBC)
library(brnn)
library(glmnet)
memory.limit(size = 4000)
z <-odbcConnect("mydb") # database with Access queries and tables

# import the data
f5 <- sqlFetch(z,"my_qry")

# head(f5)

# check for 'NA'
sum(is.na(f5))

# choose a 'locn', up to 16 of variable 'locn' are present
f6 <- subset(f5, locn == "mm")
# dim(f6)

# use glmnet to identify possible iv's

training_xnm <- f6[,1:52] # training data
xnm <- as.matrix(training_xnm)
y <- f6[,54] # response

fit.nm <- glmnet(xnm,y, family="binomial", alpha=0.6, nlambda=1000,standardize=TRUE,maxit=100000)
# print(fit.nm)

# cross validation for glmnet to determine a good lambda value
cv.fit.nm <- cv.glmnet(xnm, y)

# have a look at the 'min' and '1se' lambda values
cv.fit.nm$lambda.min
cv.fit.nm$lambda.1se
# returned $lambda.min of 0.002906279, $lambda.1se of 2.587214

# for testing purposes I choose a value between 'min' and '1se'
mid.lambda.nm = (cv.fit.nm$lambda.min + cv.fit.nm$lambda.1se)/2

print(coef(fit.nm, s = mid.lambda.nm)) # 8 iv's retained

# I then manually inspect the data frame and enter the column index for each of the iv's
# these iv's will be the input to my 'brnn' neural nets

cols <- c(1, 3, 6, 8, 11, 20, 25, 38) # column indices of useful iv's

# brnn creation: only one shown but this step will be repeated
# take a 85% sample from data frame
ridxs <- sample(1:nrow(f6), floor(0.85*nrow(f6)) ) # row id's
f6train <- f6[ridxs,] # the resultant data frame of 85%
f6train <-f6train[,cols] # 'cols' as chosen above

# For the 'brnn' phase response is a binary value, 'fin'
# and predictors are the 8 iv's found earlier
out = brnn( fin ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8, data=f6train, neurons=3,normalize=TRUE, epochs=500, verbose=FALSE)
#summary(out)

# see how well the net predicts the training cases
pred <- predict(out)

Das obige Skript läuft OK.

Meine Frage ist: Wie kann ich automatisieren, die oben genannten Skript für verschiedene Werte von locn, dass ist im wesentlichen, wie kann ich das verallgemeinern erste Schritt: cols <- c(1, 3, 6, 8, 11, 20, 25, 38) # column indices of useful iv's. Derzeit kann ich das manuell tun, aber nicht sehen können, wie Sie dies in einer Allgemeinen Art und Weise für verschiedene Werte von locn zum Beispiel

locn.list <- c("am", "bm", "cm", "dm", "em")  
for(j in 1:5) {
this.locn <- locn.list[j]
# run the above script
}
  • Es sieht nicht wie eine Prüfung mit Ihren Daten ist möglich, jedoch sollten Sie unmittelbar erfahren, dass mit "(" nach einem token macht R-look für eine Funktion mit diesem Namen. Wahrscheinlich wollen locn.list[j]. Die j<-1 Zeile erscheint völlig überflüssig.
  • Vielen Dank für Kommentar DWin: mein schlechtes, Tippfehler, und ja, ich Stimme j < 1 ist überflüssig!
  • Vielen Dank für Kommentar DWin: mein schlechtes, Tippfehler, und ja, ich Stimme j < 1 ist überflüssig! Es ist kein problem, läuft der code wie ich bereits erwähnt, meine Frage war, wie die Verallgemeinerung der Sammlung der nützlichen Variablen aus glmnet nach Kreuzvalidierung. Derzeit verwende ich den code, viele Male pro Tag anhand von live-finanziellen Daten für einen Wert, der 'locn'. Ich könnte ein separates Skript für alle 17 Werte von 'locn' und führen Sie diese in Reihe, aber ich hatte gehofft, zu erfassen, die Zeile beginnt: cols <- c(1,...... programmatisch haben, anstatt manuell Eingabe dieser Zeile für jedes 'locn'.
  • Sollten Sie Bearbeiten Ihre Frage, wenn Sie einverstanden sind, dass die Fehler in Ihrem code. Ich bin daran interessiert, das problem wenn Sie sehen können, Ihren Weg klar zu machen das dataset zur Verfügung.
  • Dank DWin, habe ich editierte meinen Beitrag, wie Sie vorschlagen.
InformationsquelleAutor cousin_pete | 2013-08-21
Schreibe einen Kommentar