在两个列表中的data.frames之间应用predict() [英] Apply predict() between data.frames within two lists

查看:255
本文介绍了在两个列表中的data.frames之间应用predict()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些示例数据:

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))

我正在应用支持向量机模型(svm):

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))

现在,我希望在svm_fit_lstlst_test data.frames之间应用prediction()函数,但是当我运行以下代码时R给我一个错误:

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)})

UseMethod("predict")中的错误:'predict'没有适用的方法 应用于列表"类的对象

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

我只希望在svm_fit_lst[1]lst_test[1]以及svm_fit_lst[2]lst_test[2]之间应用predict()函数.

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].

有什么建议吗? 谢谢

推荐答案

因为您需要遍历两个列表,所以请考虑使用Map(mapply的包装器)而不是lapply:

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)

等效地:

svm_pred_lst = mapply(function(s, l) predict(s, newdata=l), svm_fit_lst, lst_test, SIMPLIFY = FALSE)

这篇关于在两个列表中的data.frames之间应用predict()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆