javed khan
2019-Dec-20 19:53 UTC
[R] contrasts can be applied only to factors with 2 or more levels
I am using the folowing code and it give me the error message like " contrasts can be applied only to factors with 2 or more levels". What could be the problem d=read.csv("Result.csv") index <- createDataPartition(log10(d$Results), p = .70,list = FALSE) tr <- d[index, ] ts <- d[-index, ] index_2 <- createFolds(log10(tr$Results), returnTrain = TRUE, list = TRUE) ctrl <- trainControl(method = "cv", index = index_2) set.seed(30218) grid_search <- train(log10(Results) ~ ., data = tr, method = "svmRadial", tuneLength = 8, metric = "MAE", preProc = c("center", "scale", "zv"), trControl = ctrl) [[alternative HTML version deleted]]
Patrick (Malone Quantitative)
2019-Dec-20 20:08 UTC
[R] contrasts can be applied only to factors with 2 or more levels
Seems self-explanatory. It sounds like one of your predictors has no variability. On Fri, Dec 20, 2019 at 3:01 PM javed khan <javedbtk111 at gmail.com> wrote:> > I am using the folowing code and it give me the error message like > > " contrasts can be applied only to factors with 2 or more levels". > > What could be the problem > > d=read.csv("Result.csv") > index <- createDataPartition(log10(d$Results), p = .70,list = FALSE) > tr <- d[index, ] > ts <- d[-index, ] > index_2 <- createFolds(log10(tr$Results), returnTrain = TRUE, list = TRUE) > ctrl <- trainControl(method = "cv", index = index_2) > > set.seed(30218) > > grid_search <- train(log10(Results) ~ ., data = tr, > method = "svmRadial", > > tuneLength = 8, > metric = "MAE", > preProc = c("center", "scale", "zv"), > trControl = ctrl) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Patrick (Malone Quantitative)
2019-Dec-20 21:20 UTC
[R] contrasts can be applied only to factors with 2 or more levels
As the Posting Guide directs, please make all replies to the list. Basically, you need to inspect your data. Using "~ ." adds everything in the dataframe that's not elsewhere in the function as predictors. Check the descriptive statistics on every one (variance/standard deviation for numeric data, table for string/factor). On Fri, Dec 20, 2019 at 3:32 PM javed khan <javedbtk111 at gmail.com> wrote:> > Hi Patrick > > I have no idea about this type of error. Is it a categorical predictor? How to find variability? > > Thanks > > On Fri, Dec 20, 2019 at 9:08 PM Patrick (Malone Quantitative) <malone at malonequantitative.com> wrote: >> >> Seems self-explanatory. It sounds like one of your predictors has no >> variability. >> >> On Fri, Dec 20, 2019 at 3:01 PM javed khan <javedbtk111 at gmail.com> wrote: >> > >> > I am using the folowing code and it give me the error message like >> > >> > " contrasts can be applied only to factors with 2 or more levels". >> > >> > What could be the problem >> > >> > d=read.csv("Result.csv") >> > index <- createDataPartition(log10(d$Results), p = .70,list = FALSE) >> > tr <- d[index, ] >> > ts <- d[-index, ] >> > index_2 <- createFolds(log10(tr$Results), returnTrain = TRUE, list = TRUE) >> > ctrl <- trainControl(method = "cv", index = index_2) >> > >> > set.seed(30218) >> > >> > grid_search <- train(log10(Results) ~ ., data = tr, >> > method = "svmRadial", >> > >> > tuneLength = 8, >> > metric = "MAE", >> > preProc = c("center", "scale", "zv"), >> > trControl = ctrl) >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code.
William Dunlap
2019-Dec-20 21:56 UTC
[R] contrasts can be applied only to factors with 2 or more levels
You can also get this error from caret::grid_search() if you have 2-valued string variables with one value pretty rare, so that a sample may include just one of the values. You can get this if you've set options(stringsAsFactors=FALSE) or call read.table() with stringsAsFactors=FALSE. E.g., in R-3.6.2:> set.seed(1) > d <- data.frame(Results=1:1000, x1=ifelse((1:1000)==1, "one", "not one"),x2=ifelse((1:1000)==2,"two", "not two"), stringsAsFactors=FALSE)> index <- createDataPartition(log10(d$Results), p = 0.25, list = FALSE) > tr <- d[index, ] > ts <- d[-index, ] > index_2 <- createFolds(log10(tr$Results), returnTrain = TRUE, list = TRUE) > ctrl <- trainControl(method = "cv", index = index_2) > grid_search <- train(log10(Results) ~ ., data = tr, method="lm",trControl=ctrl) Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels Converting all character columns to factor columns can help. Bill Dunlap TIBCO Software wdunlap tibco.com On Fri, Dec 20, 2019 at 1:21 PM Patrick (Malone Quantitative) < malone at malonequantitative.com> wrote:> As the Posting Guide directs, please make all replies to the list. > > Basically, you need to inspect your data. Using "~ ." adds everything > in the dataframe that's not elsewhere in the function as predictors. > Check the descriptive statistics on every one (variance/standard > deviation for numeric data, table for string/factor). > > On Fri, Dec 20, 2019 at 3:32 PM javed khan <javedbtk111 at gmail.com> wrote: > > > > Hi Patrick > > > > I have no idea about this type of error. Is it a categorical predictor? > How to find variability? > > > > Thanks > > > > On Fri, Dec 20, 2019 at 9:08 PM Patrick (Malone Quantitative) < > malone at malonequantitative.com> wrote: > >> > >> Seems self-explanatory. It sounds like one of your predictors has no > >> variability. > >> > >> On Fri, Dec 20, 2019 at 3:01 PM javed khan <javedbtk111 at gmail.com> > wrote: > >> > > >> > I am using the folowing code and it give me the error message like > >> > > >> > " contrasts can be applied only to factors with 2 or more levels". > >> > > >> > What could be the problem > >> > > >> > d=read.csv("Result.csv") > >> > index <- createDataPartition(log10(d$Results), p = .70,list = FALSE) > >> > tr <- d[index, ] > >> > ts <- d[-index, ] > >> > index_2 <- createFolds(log10(tr$Results), returnTrain = TRUE, list > TRUE) > >> > ctrl <- trainControl(method = "cv", index = index_2) > >> > > >> > set.seed(30218) > >> > > >> > grid_search <- train(log10(Results) ~ ., data = tr, > >> > method = "svmRadial", > >> > > >> > tuneLength = 8, > >> > metric = "MAE", > >> > preProc = c("center", "scale", "zv"), > >> > trControl = ctrl) > >> > > >> > [[alternative HTML version deleted]] > >> > > >> > ______________________________________________ > >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> > https://stat.ethz.ch/mailman/listinfo/r-help > >> > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > >> > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]