"argument is of length zero" error in RandomForest tuneRF() - r

I used this code,
tuneRF(x = mtry[,c(4:18)],
y = mtry$Os, ntreeTry=50, stepFactor=2,
improve=0.05,trace=TRUE, plot=TRUE, doBest=FALSE)
And got the error:
Error in if (errorOld < 0) stop("Initial setting gave 0 error and no room for improvement.") :
argument is of length zero
What should I change?

I ran into this same error when my I accidentally typed a column that didn't exist for the response column. Perhaps your mtry$Os column does not exist (it's hard to solve your problem w/o a reproducible example).
Example:
> library(randomForest)
> data("iris")
> tuneRF(x = iris[1:4,], y = iris$notacol)
Error in if (errorOld < 0) stop("Initial setting gave 0 error and no room for improvement.") :
argument is of length zero

Related

Error in knn(Week.train, Week.test, train.Direction, k = 1) : no missing values are allowed, i don't know how to fix this error

library(class)
Week.train=as.matrix(Lag2[data1])
Week.test=as.matrix(Lag2[!data1])
train.Direction =Direction[data1]
set.seed(1)
Weekknn.pred=knn(Week.train,Week.test,train.Direction,k=1)
table(Weekknn.pred,Direction.0910)
Error in knn(Week.train, Week.test, train.Direction, k = 1) :
no missing values are allowed

"Error: Matrix must have equal dimensions" despite seemingly equal dimensions

pred <- predict(fit, x, type="response", s=cv$lambda.min)
confusion_matrix <- confusionMatrix(data = pred, reference = testXsp)
Error in confusionMatrix.matrix(data = pred, reference = testXsp) :
matrix must have equal dimensions
dim(pred)
[1] 751864 1
dim(testXsp)
[1] 751864 1
dim(testXsp) == dim(pred)
[1] TRUE TRUE
The dimensions seem to be the same, then why am I getting this error message?
confusionMatrix argument data must be square if it is a matrix.
> caret:::confusionMatrix.matrix
function (data, positive = NULL, prevalence = NULL, mode = "sens_spec",
...)
{
if (length(unique(dim(data))) != 1) {
stop("matrix must have equal dimensions")
}
classTable <- as.table(data, ...)
confusionMatrix(classTable, positive, prevalence = prevalence,
mode = mode)
}
<bytecode: 0x126452f88>
<environment: namespace:caret>
Note that the method for class matrix does not even take a reference argument. It is the default method that uses reference. Perhaps you should review the help page for confusionMatrix?
One possibility here is that there are one or more NA values contained in your prediction matrix. Try using the following command:
na.omit(pred)
Afterwards, rerun the above code. If this does not work, please post the package you are using to fit your model. This will allow for a more detailed solution!
Best wishes,
-Matt

R: error in tableplot: Error in if (by < 1) stop("'by' must be > 0")

I'm a beginner in R and cannot understand what's the problem in that simple code:
install.packages("tabplot")
library("tabplot")
library("MASS")
Boston$chas <- factor(Boston$chas)
Boston$rad <- ordered(Boston$rad)
tableplot(Boston)
After running the function 'tableplot' I get this error message:
Error in if (by < 1) stop("'by' must be > 0") :
missing value where TRUE/FALSE needed
`In addition: Warning message:
In chunk.default(from = 1L, to = 506L, by = c(double = 23058430092136940), :NAs introduced by coercion to integer range
What is the problem? There're no missing values in the dataset. Could someone explain it, please?
Many thanks in advance
Daria

PCA with result non-interactively in R

I send you a message because I would like realise an PCA in R with the package ade4.
I have the data "PAYSAGE" :
All the variables are numeric, PAYSAGE is a data frame, there are no NAS or blank.
But when I do :
require(ade4)
ACP<-dudi.pca(PAYSAGE)
2
I have the message error :
**You can reproduce this result non-interactively with:
dudi.pca(df = PAYSAGE, scannf = FALSE, nf = NA)
Error in if (nf <= 0) nf <- 2 : missing value where TRUE/FALSE needed
In addition: Warning message:
In as.dudi(df, col.w, row.w, scannf = scannf, nf = nf, call = match.call(), :
NAs introduced by coercion**
I don't understand what does that mean. Have you any idea??
Thank you so much
I'd suggest sharing a data set/example others could access, if possible. This seems data-specific and with NAs introduced by coercion you may want to check the type of your input - typeof(PAYSAGE) - the manual for dudi.pca states it takes a data frame of numeric values as input.
Yes, for example :
ag_div <- c(75362,68795,78384,79087,79120,73155,58558,58444,68795,76223,50696,0,17161,0,0)
canne <- c(rep(0,10),5214,6030,0,0,0)
prairie_el<- c(60, rep(0,13),76985)
sol_nu <- c(18820,25948,13150,9903,12097,21032,35032,35504,25948,20438,12153,33096,15748,33260,44786)
urb_peu_d <- c(448,459,5575,5902,5562,458,6271,6136,459,1850,40,13871,40,13920,28669)
urb_den <- c(rep(0,12),14579,0,0)
veg_arbo <- c(2366,3327,3110,3006,3049,2632,7546,7620,3327,37100,3710,0,181,0,181)
veg_arbu <- c(18704,18526,15768,15527,15675,18886,12971,12790,18526,15975,22216,24257,30962,24001,14523)
eau <- c(rep(0,10),34747,31621,36966,32165,28054)
PAYSAGE<-data.frame(ag_div,canne,prairie_el,sol_nu,urb_peu_d,urb_den,veg_arbo,veg_arbu,eau)
require(ade4)
ACP<-dudi.pca(PAYSAGE)

Create data for portfolio in R

I have data in Excel. Suppose I read it like this (only one series is shown below):
ccl<-ts(mysheets$CCL$`Adj Close`,start=c(2000, 1), end=c(2012, 12), frequency=12)
ccl.r<-diff(log(ccl), lag=1)
Then, I construct a vector with all the data:
data<-cbind(aal.r, adm.r, aht.r, anto.r, arm.r, av.r, azn.r, ba.r, bab.r, barc.r, bats.r,bdev.r, bkg.r, blnd.r, blt.r, bnzl.r, bta.r, bznl.r, ccl.r)
Then, I try to insert the data into format of fportfolio, by using:
ewSpec<-portfolioSpec()
nAssets<-ncol(data)
setWeights(ewSpec)<-rep(1/nAssets, time=nAssets)
mydata<-portfolioData(data=data, spec=portfolioSpec())
However, I get this error:
Error in portfolioData(data = data, spec = portfolioSpec()) :
object 'assetsNames' not found
In addition: Warning messages:
1: In if (class(data) == "timeSeries") { :
the condition has length > 1 and only the first element will be used
2: In if (class(data) == "list") { :
the condition has length > 1 and only the first element will be used
This was solved by making the matrix a "timeSeries" object. Thanks for reading the question...

Resources