Reputation: 141
mydat=structure(list(spent = c(73.5, 73.5, 73.5, 73.5, 73.5, 73.5,
73.5, 73.5, 73.5, 73.5, 73.5, 29.74, 29.74, 29.74, 29.74, 29.74,
29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 73.71, 73.71, 73.71,
73.71, 73.71, 73.71, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5,
73.5, 73.5, 73.5, 73.5, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74,
29.74, 29.74, 29.74, 29.74, 29.74, 73.71, 73.71, 73.71, 73.71,
73.71, 73.71), realpurchase_cash = c(501, 502, 503, 504, 505,
506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518,
519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 73.5, 73.5,
73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 29.74,
29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74, 29.74,
29.74, 73.71, 73.71, 73.71, 73.71, 73.71, 73.71), id = c(123L,
123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L,
123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L, 123L,
123L, 123L, 123L, 123L, 123L, 124L, 124L, 124L, 124L, 124L, 124L,
124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L,
124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L, 124L
), flag = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("spent",
"realpurchase_cash", "id", "flag"), class = "data.frame", row.names = c(NA,
-56L))
i want perform Linear discriminant analysis. (lda function comes from MASS library)
here my code
index <- sample(1:nrow(mydat),round(0.70*nrow(mydat)))
train <- mydat[index,]
test <- mydat[-index,]
library("MASS")
fit=lda(flag~spent+realpurchase_cash, mydat, subset = train)
library("caret")
str(mydat)
library(InformationValue)
predicted <- predict(fit,test,type='response')
optCutOff <- optimalCutoff(test$flag, predicted)[1]
confusionMatrix(test$flag, predicted, threshold = optCutOff)
but after this string
fit=lda(flag~spent+realpurchase_cash, mydat, subset = train)
i get the error
Error in xj[i] : invalid subscript type 'list'
How to fix this error. What's wrong?
Upvotes: 2
Views: 1762
Reputation: 72623
subset
wants a vector and you give him a data frame (i.e. a list). Do
lda(flag ~ spent + realpurchase_cash, mydat, subset=index)
or, since train
is already your desired subset, just
lda(flag ~ spent + realpurchase_cash, data=train)
Upvotes: 2