Trapping error in R - r

A very basic quesiton. But i am not able to apply this to my code. Hence seeking help here
I am getting an error mentioned below while running this R code
knn.pred <- knn(tdm.stack.nl_train, tdm.stack.nl_Test, tdm.cand_train, prob = TRUE)
> Error in knn(tdm.stack.nl_train, tdm.stack.nl_Test, tdm.cand_train, prob = TRUE) :
> dims of 'test' and 'train' differ.
I want to print the error message as given below. However I could not achieve this. I am not good in writing functions yet.. Please help.
out <- tryCatch( when error = {print('New words seen in testing data')})

It's better and easier to use try:
knn.pred <- try(knn(tdm.stack.nl_train, tdm.stack.nl_Test, tdm.cand_train, prob = TRUE))
if (inherits(knn.pred, "try-error") { # error management
print('New words seen in testing data')
}

You could do:
tryCatch(knn.pred <- knn(tdm.stack.nl_train, tdm.stack.nl_Test, tdm.cand_train, prob = TRUE),
error = function(e) {
stop('New words seen in testing data')
})
This shows up as:
tryCatch(knn.pred <- knn(tdm.stack.nl_train, tdm.stack.nl_Test, tdm.cand_train, prob = TRUE),
error = function(e) {
stop('New words seen in testing data')
})
Error in value[[3L]](cond) : New words seen in testing data

Related

I am using ALDEx package for analysis of PICRUST2 output and gave me an error

I am following the tutorial below in the link:
https://ycl6.github.io/16S-Demo/4_picrust2_tutorial.html
after copying this code :
# EC
set.seed(12345)
system.time({
aldex2_EC1 = aldex(p2EC1, sample_data(ps1a)$Group, mc.samples = 500, test = "t",
effect = TRUE, denom = "iqlr", verbose = TRUE)
})
it gives me an error : Error in aldex.clr.function(as.data.frame(reads), conds, mc.samples, denom, :
mismatch between number of samples and condition vector.
I am working on 5 or 6 samples..
I don't know what is the cause of the error
I have no solution in google.

arulesViz subscript out of bounds paracoord

I want to perform basket analysis and draw a paracoord plot however I receive an error.
Content of this error is: :
Error in m[j, i] : subscript out of bounds.In addition: Warning message:
In cbind(pl, pr) :
number of rows of result is not a multiple of vector length (arg 2)
I am using data from: Link.
First I am transforming this to fit basket analysis, name of the original excel files is Online_Retail:
library(arules)
library(arulesViz)
library(plyr)
items <- ddply(Online_Retail, c("CustomerID", "InvoiceDate"), function(df1)paste(df1$Description, collapse = ","))
items1 <- items["V1"]
write.csv(items1, "groceries1.csv", quote=FALSE, row.names = FALSE, col.names = FALSE)
trans1 <- read.transactions("groceries1.csv", format = "basket", sep=",",skip=1)
And to draw paracoord I have created such a code:
rules.trans2<-apriori(data=trans1, parameter=list(supp=0.001,conf = 0.05),
appearance=list(default="rhs", lhs="ROSES REGENCY TEACUP AND SAUCER"), control=list(verbose=F))
sorted.plot <- sort(rules.trans2, by="support", decreasing = TRUE)
plot(sorted.plot, method="paracoord", control=list(reorder=TRUE, verbose = TRUE))
Why my code for paracoord is not working? how can I fix it? What should I change?
This is, unfortunately, a bug in arulesViz. This will be fixed in the next release (arulesViz 1.3-3). The fix is already available in the development version on GitHub: https://github.com/mhahsler/arulesViz

Basic ntile function to create decile portfolios

https://imgur.com/a/O1O9G
I try to create decile portfolios based on momentum. I use DAX in Germany.
dax <- read.csv("DAXclean.csv", header = TRUE, sep = ";", dec = ",")
as.ts(dax)
mom.return <- matrix(NA,nrow(dax),ncol(dax))
mom.decile <- matrix(NA,nrow(dax),ncol(dax))
for (row in 13:nrow(dax)) {
for (column in 2:ncol(dax)) {
mom.return[row,column] <- (dax[row-1,column]-dax[row-12,column])/dax[row-12,column]
}
mom.decile[row,column] <- ntile(mom.return[row,2:ncol(dax)], 10)
}
When I run this code I get the following error message:
"Error in `[<-`(`*tmp*`, row, column, value = ntile(mom.return[row, 2:ncol(dax)], :
subscript out of bounds"
If I remove the following command, everything works fine.
mom.decile[row,column] <- ntile(mom.return[row,2:ncol(dax)], 10)
I can't see what the problem is.
Thank you in advance!

Error in parsing data on R-Studio while running ADF

I cannot seem to get past or understand what is wrong with this error while trying to run the ADF (Augmented Dickey Fuller) test on R-Studio. Will appreciate comments.
library(AER)
library("dynlm", lib.loc="~/R/win-library/3.2")
x<-read.table("C://R Files/protein.csv", header=T, sep=",")
"adf" <- function(x,k = 0, int = TRUE, trend = FALSE){
require(dynlm)
dx <- diff(x)
formula <- paste("dx ~ L(x)")
if(k > 0)
formula <- paste(formula," L(dx,1:k)")
if(trend){
s <- time(x)
t <- ts(s - s[1],start = s[1],freq = frequency(x))
formula <- paste(formula," t")
}
if(!int) formula <- paste(formula," - 1")
summary(dynlm(as.formula(formula)))
}
a<-ts(x$a)
adf(a, k=1, int=T, trend=T)
The error message that I get after this is :
Error in parse(text = x, keep.source = FALSE) :
:1:13: unexpected symbol
1: dx ~ L(x) L
^
It might be this:
formula <- paste(formula," L(dx,1:k)")
You can't just add something to a model. Try doing:
formula <- paste(formula,"+ L(dx,1:k)")
and see if that helps. If not you might want to share the contents of protein.csv so I can try to reproduce your problem.

Mann-Whitney-Wilcoxon test in R giving Error

I am trying to run a Mann-Whitney test across large data set. Here is an excerpt of my input:
GeneID GeneID-2 GeneName TSS-ID Locus-ID TAp73fTfTAAdEmp TAp73fTfTFAdEmp TAp73fTfTJAdEmp TAp73fTfTAAdCre TAp73fTfTFAdCre TAp73fTfTJAdCre
ENSMUSG00000028180 ENSMUSG00000028180 Zranb2 TSS1050,TSS17719,TSS52367,TSS53246,TSS72833,TSS73222 3:157534159-157548390 11.32013333 11.66344 11.87956667 13.01974667 14.70944667 10.94043867
ENSMUSG00000028184 ENSMUSG00000028184 Lphn2 TSS23298,TSS2403,TSS74519 3:148815585-148989316 15.0983 15.09572 14.03578667 17.00742667 17.90735333 14.69675333
ENSMUSG00000028187 ENSMUSG00000028187 Rpf1 TSS66485 3:146506347-146521423 12.34542667 14.11470667 10.493766 14.57954 11.93746667 11.07405867
ENSMUSG00000028189 ENSMUSG00000028189 Ctbs TSS36674,TSS72417 3:146450469-146465849 1.288003867 1.435658 1.959620667 1.427768 1.502116667 1.243928267
ENSMUSG00000020755 ENSMUSG00000020755 Sap30bp TSS14892,TSS218,TSS54781,TSS58430 11:115933281-115966725 31.91070667 31.68585333 26.86939333 39.05116667 30.62916667 27.22893333
ENSMUSG00000020752 ENSMUSG00000020752 Recql5 TSS26689,TSS42686,TSS60902,TSS75513,TSS9111 11:115892594-115933477 10.55415467 9.373216667 8.315984 7.255579333 7.022178 8.553787333
ENSMUSG00000020758 ENSMUSG00000020758 Itgb4 TSS23937,TSS28540,TSS29211,TSS34600,TSS36953,TSS4070,TSS6591,TSS68296 11:115974708-116008412 130.2124 117.3862 129.323 134.1108667 134.8743333 165.3330667
ENSMUSG00000069833 ENSMUSG00000069833 Ahnak TSS54612 19:8989283-9076919 116.3223333 135.2628 130.1286 147.045 142.8164 127.2352
ENSMUSG00000033863 ENSMUSG00000033863 Klf9 TSS87300 19:23141225-23166911 23.23418667 27.46006 26.56143333 21.09004667 18.47022 16.63767333
ENSMUSG00000069835 ENSMUSG00000069835 Sat2 TSS71535,TSS9615 11:69622023-69623870 0.975045133 0.886760067 1.593631333 1.469496 1.2373384 1.292182733
ENSMUSG00000028233 ENSMUSG00000028233 Tgs1 TSS24151,TSS28446,TSS50213,TSS68499,TSS79096 4:3574874-3616619 4.221024667 4.212087333 4.160574 5.113266667 6.917347333 5.22148
ENSMUSG00000028232 ENSMUSG00000028232 Tmem68 TSS12134,TSS25773,TSS25778,TSS49743,TSS7797 4:3549040-3574853 4.048868 3.906129333 6.024607333 4.613682 6.292972 4.287184
I wrote the same script for t-test and it worked. However the replacing test by "wilcox" is giving me the error:
Error in wilcox.test.default(x[i, 1:3], x[i, 4:6], var.equal = TRUE) :
'x' must be numeric
My code is:
library(preprocessCore)
err <-file("err.Rout", open="wt")
sink(err, type="message")
x <- read.table("Data.txt", row.names=1, header=TRUE, sep="\t", na.strings="NA")
x<-x[,5:ncol(x)]
p<-matrix(0,nrow(x),3)
for (i in 1:nrow(x)) {
myTest <- try(wilcox.test(x[i,1:3], x[i,4:6], var.equal=TRUE))
if (inherits(myTest, "try-error"))
{ p[i,2]=1 }
else
{p[i,2]=myTest$p.value; num=rowMeans(x[i,1:3], na.rm = FALSE); den=rowMeans(x[i,4:6], na.rm = FALSE); ratio=num/den; p[i,1]=ratio }
}
p[,3] = p.adjust(p[,2], method="none")
colnames(p) <- c("FoldChange", "p-value", "Adjusted-p")
write.table(p, file = "tmpPval-fold.txt", append = FALSE, quote = FALSE, sep = "\t", row.names = FALSE, col.names = TRUE)
sink()
I'd appreciate your help in this matter. As i said, it worked perfectly if I use test instead of 'wilcox'.
There are (at least) two problems with your code at the moment, one of them is the cause of that error. The class of the object returned by x[i,1:3] is data.frame which is a list object and fails the is.numeric test inside wilcox.test. Try coercing:
wilcox.test(as.numeric(x[1,(1:3)]), as.numeric(x[1,(4:6)]), var.equal=TRUE)
But what-the-F is var.equal doing in a call to a non-parametric test that will not have any assumption of equal variance? (Actually it is getting ignored is what is happening.) And how do you expect to be getting useful information from a test when you're only giving 3 items compared to 3 items. That is never giving to be "significant" or even particularly informative. I doubt that a t.test could be informative when it is 3 vs 3 but a non-parametric test that is based on ordering of values is going to be even less likely to give a statistical signal of "significance".

Resources