Apply predict() between data.frames within two lis

2019-02-15 19:28发布

Here some example data:

df_1 = read.table(text = 'Year  count  var1
                        1951       12  380 
                        1952       13  388 
                        1953       11  400 
                        1954       14  411 
                        1955       14  422 
                        1956       14  437 
                        1957       12  451 
                        1958       14  465 
                        1959       13  481 
                        1960       15  502 
                        1961       17  522 
                        1962       16  549 
                        1963       14  572 
                        1964       16  580', header = TRUE)

df_2 = read.table(text = 'Year count  var1
                         1951       12  380 
                         1952       13  388 
                         1953       11  400 
                         1954       15  411 
                         1955       14  422 
                         1956       15  437 
                         1957       11  451 
                         1958       14  465 
                         1959       13  481 
                         1960       15  502 
                         1961       20  522 
                         1962       17  549 
                         1963       14  572 
                         1964       16  592', header = TRUE)

lst1 = list(df_1, df_2)


#split data.frames within lst1 and create training and testing lists
lst_train = lapply(lst1, function(x) subset(x, Year < 1959))
lst_test = lapply(lst1, function(x) subset(x, Year > 1958))

I am applying the support vector machine model (svm):

library(e1071)

#run SVM model for all data.frames within lst_train
svm_fit_lst = lapply(lst_train, function(x) svm(count ~ var1, data = x))

Now I desire to apply the prediction() function between svm_fit_lst and lst_test data.frames but R gives me an error when I run the following code:

svm_pred_lst = lapply(lst_test, function(x) {predict(svm_fit_lst, newdata = x)})

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "list"

I just desire the predict() function to be applied between svm_fit_lst[1] and lst_test[1], and svm_fit_lst[2] and lst_test[2].

Any suggestion? Thanks

2条回答
姐就是有狂的资本
2楼-- · 2019-02-15 20:12

Because you need to iterate through two lists, consider Map (wrapper of mapply) instead of lapply:

svm_pred_lst = Map(function(s, l) predict(s, newdata=l), svm_fit_lst, lst_test)

Equivalently:

svm_pred_lst = mapply(function(s, l) predict(s, newdata=l), svm_fit_lst, lst_test, SIMPLIFY = FALSE)
查看更多
可以哭但决不认输i
3楼-- · 2019-02-15 20:18

Because we are dealing with a sequential problem here, it is best to write a for loop to accomplish the task:

result <- list()
for (i in 1:length(svm_fit_lst)){
        result[[i]] <- predict(svm_fit_lst[[i]], 
                               newdata = lst_test[[i]])
}

#Test
result
#[[1]]
#       9       10       11       12       13       14 
#13.94310 13.69655 13.55169 13.52698 13.52656 13.52656 
#
#[[2]]
#       9       10       11       12       13       14 
#13.84789 13.67391 13.55716 13.53580 13.53542 13.53542 
查看更多
登录 后发表回答