Вопрос

I am using glm() to create a few different models based on the values in a vector I make (h1_lines). I want sapply to return a model for each value in the vector. Instead, my code is currently returning a list of lists where one part of the list is the model. It seems to be returning everything I do inside the sapply function.

train = data.frame(scores=train[,y_col], total=train[,4], history=train[,5], line=train[,6])
h1_lines<- c(65, 70, 75)

models <- sapply(h1_lines, function(x){
                 temp_set<-train
                 temp_set$scores<-ifelse(temp_set$scores>x,1,
                                     ifelse(temp_set$scores<x,0,rbinom(dim(temp_set)[1],1,.5)))

                 mod<-glm(scores ~ total + history + line, data=temp_set, family=binomial)
                                    })

I'd like the code to work so after these lines I can do:

predict(models[1,], test_case)
predict(models[2,], test_case)
predict(models[3,], test_case)

But right now I can't do it cause sapply is returning more than just the model... If I do print(dim(models)) it says models has 30 rows and 3 columns??

EDIT TO ADD QUESTION;

Using the suggestion below code works great, I can do predict(models[[1]], test_case) and it works perfectly. How can I return/save the models so I can access them with the key I used to create them? For example, using the h1_scores it could be something like the following:

predict(models[[65]], test_case))

predict(models[[key==65]], test_case)
Это было полезно?

Решение

You need to use lapply instead of sapply.

sapply simplifies too much. Try:

lapply(ListOfData, function(X) lm(y~x, X))
sapply(ListOfData, function(X) lm(y~x, X))

I don't know exactly the distinction, but if you're ever expect the output of each item of sapply to have extractable parts (i.e. Item$SubItem), you should use lapply instead.

Update

Answering your next question, you can do either:

names(models) <- h1_lines
names(h1_lines) <- h1_lines ## Before lapply

And call them by

models[["65"]]

Remember to use quotes around the numbers. As a side note, naming list items with numbers is not always the best idea. A workaround could be:

models[[which(h1_lines==65)]]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top