在两个列表中的 data.frames 之间应用 predict()
Apply predict() between data.frames within two lists
这里有一些示例数据:
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):
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_lst
和 lst_test
data.frames 之间应用 prediction()
函数但是当我 运行 以下代码时 R 给我一个错误:
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"
我只希望在 svm_fit_lst[1]
和 lst_test[1]
以及 svm_fit_lst[2]
和 lst_test[2]
之间应用 predict()
函数。
有什么建议吗?
谢谢
因为我们这里处理的是顺序问题,所以最好写一个for循环来完成任务:
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
因为您需要遍历两个列表,请考虑 Map
(mapply
的包装)而不是 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)
这里有一些示例数据:
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):
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_lst
和 lst_test
data.frames 之间应用 prediction()
函数但是当我 运行 以下代码时 R 给我一个错误:
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"
我只希望在 svm_fit_lst[1]
和 lst_test[1]
以及 svm_fit_lst[2]
和 lst_test[2]
之间应用 predict()
函数。
有什么建议吗? 谢谢
因为我们这里处理的是顺序问题,所以最好写一个for循环来完成任务:
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
因为您需要遍历两个列表,请考虑 Map
(mapply
的包装)而不是 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)