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
svm_fit_lst
, which is alist
and that's why you get the error. So this will work:{predict(svm_fit_lst[[1]], newdata = x)}
– mtoto