R: overwrite rules that apriori produced - r

I want to overwrite confidence values of an apriori output, then put the output into is.redundant. I got an error at the last line. How do you do it?
library(arules)
data(Groceries) # read sample data
# find apriori rules
outApriori = apriori(Groceries,
parameter = list(support=0.001, confidence=0.70, minlen=1, maxlen=4)
,appearance = list(rhs = "whole milk" ) )
dfApriori = as.data.frame(inspect(outApriori[1:5])) # convert into data.frame
# modify the confidence value conservatively by adding one error sample
(estimateConfidence= dfApriori$count / (1 + round( dfApriori$count / dfApriori$confidence ) ))
dfApriori$confidence = estimateConfidence
outRmRedundant <- dfApriori[!is.redundant(dfApriori)] # Error in (function (classes, fdef, mtable) :
# Error in (function (classes, fdef, mtable) :
# unable to find an inherited method for function ‘is.redundant’ for signature ‘"data.frame"’

The function is.redundant() expects a rules object not a data.frame. Here is how you change the quality slot of the rules object:
library(arules)
data(Groceries)
# find apriori rules
rules <- apriori(Groceries,
parameter = list(support=0.001, confidence=0.70, minlen=1, maxlen=4),
appearance = list(rhs = "whole milk"))
estimatedConfidence <- quality(rules)$count / (1 + round(quality(rules)$count / quality(rules)$confidence))
quality(rules)$confidence <- estimatedConfidence
rules.nonredundant <- rules[!is.redundant(rules)]
inspect(head(rules.nonredundant))
BTW: You might want to look at Laplace Corrected Confidence (http://michael.hahsler.net/research/association_rules/measures.html#laplace) which can be calculated using the function interestMeasure().

Related

Normalization of cel. files after filtering of non-expressed genes in R

Before normalizing my data, I want to perform a filtering where genes that are not expressed are deleted. For this purpose I have specified a threshold value. I would like to do this filtering before normalizing and then normalize it.
library(limma)
library(hgu133plus2cdf)
library(affy)
library(dplyr)
library(oligo)
setwd("C:/A549_ALI/4_tert-Butanol (22)/")
data=read.celfiles(list.celfiles())
eset <- rma(data, normalize=FALSE, background=FALSE)
not_expressed_threshold <- quantile(exprs(eset),0.1)
not_expressed <- exprs(eset) < not_expressed_threshold
not_expressed_2 <- rownames(exprs(eset))[not_expressed]
celfiles_filtered <- eset[!rownames(eset) %in% not_expressed_2, ]
cat("Dimensions of celfiles:", dim(eset), "\n")
cat("Dimensions of celfiles_filtered:", dim(celfiles_filtered), "\n")
eset_ <- rma(celfiles_filtered, normalize=FALSE, background=FALSE)
I get this error message:
Error in (function (classes, fdef, mtable) :
cannot find inherited method for function 'rma' for signature '"ExpressionSet"'.
I tried to filter data directly and then normalize it, but it didn't work either. Thanks for any reply.

unable to find an inherited method for function 'distance' for signature '"matrix", "character"', but it's a df?

Here is all my code leading up to the function as per #Till request!
library("phyloseq")
library("qiime2R")
library("vegan")
# read in phyloseq objects from qiime
physeq<-qza_to_phyloseq(
features="~/Documents/qiime2-analyses/CRD/fresh_run/table.qza",
tree="~/Documents/qiime2-analyses/CRD/fresh_run/rooted-tree.qza",
"~/Documents/qiime2-analyses/CRD/fresh_run/taxonomy.qza",
metadata = "crd-metadata.txt")
# Clean out unwanted taxa annotations. Base script removes endozoicimonaceae, escherischia,
# and shigella contaminates
physeq <- subset_taxa(physeq, Family!="f__Endozoicimonaceae")
physeq <- subset_taxa(physeq, Family!="f__Enterobacteriaceae")
physeq <- subset_taxa(physeq, Family!="f__mitochondria")
physeq <- subset_taxa(physeq, Class!="c__Chloroplast")
#pull out otu table
otu_table <- (as.data.frame(otu_table(physeq)))
# rotate otu matrix layout
rownames(otu_table) <- factor(rownames(otu_table), levels = rownames(otu_table))
otu_ord <- as.data.frame(t(otu_table[rowSums(otu_table)!=0, ]))
# remove any rows or columns with only 0s
otu_ord <- otu_ord[, colSums(otu_ord !=0)>0]
otu_ord <- otu_ord[rowSums(otu_ord[])>0,]
#edits from observations of this StO chat
rownames(otu_ord) <- gsub("sample-", "", rownames(otu_ord))
rownames(otu_ord) <- as.numeric(rownames(otu_ord))
otu_ord <- as.matrix(otu_ord)
#the args of the function
raup_crick_abundance = function(spXsite=otu_ord, plot_names_in_col1=TRUE,
classic_metric=FALSE, split_ties=TRUE,
reps=9999, set_all_species_equal=FALSE,
as.distance.matrix=TRUE, report_similarity=FALSE){
Please note the whole function is verbatim from Stegen_etal_ISME_2013 github linked below and here.
I am receiving the error
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘distance’ for signature ‘"matrix", "character"’
Called from: (function (classes, fdef, mtable)
{
methods <- .findInheritedMethods(classes, fdef, mtable)
if (length(methods) == 1L)
return(methods[[1L]])
else if (length(methods) == 0L) {
cnames <- paste0("\"", vapply(classes, as.character,
""), "\"", collapse = ", ")
stop(gettextf("unable to find an inherited method for function %s for signature %s",
sQuote(fdef#generic), sQuote(cnames)), domain = NA)
}
else stop("Internal error in finding inherited methods; didn't return a unique method",
domain = NA)
})(list("matrix", "character"), new("nonstandardGenericFunction",
.Data = function (physeq, method, type = "samples", ...)
{
standardGeneric("distance")
}, generic = "distance", package = "phyloseq", group = list(),
valueClass = character(0), signature = c("physeq", "method",
"type"), default = NULL, skeleton = (function (physeq, method,
type = "samples", ...)
stop("invalid call in method dispatch to 'distance' (no default method)",
domain = NA))(physeq, method, type, ...)), <environment>)
Browse[1]> traceback()
No traceback available
within this function linked here.
My argument is a data.frame (dput() below) with no character strings? As I understand it, the error is saying the function distance () can't be calculated with matrix or character strings, which I agree with...
Therefore, I am unsure why distance () cannot operate with my arg, if I am interpreting the error correctly.
Thank you.
Here is my Qiime2 OTU table.qza linked to this Dropbox file, my rooted tree linked here, and my taxonomy linked here to recreate my phyloseq object.
What the error message is actually trying to get across is that your matrix has character values and the function you called can not handle it. The first column in data.frame is a character column.
If your data frame is called ex_otu_ord, try this:
ex_otu_ord <- ex_otu_ord[-1]
ex_otu_ord <- as.matrix(ex_otu_ord)
Then try again to call the function on ex_otu_ord.

How can I create an Item Frequency Plot for association rule data? By converting to data frame or numeric?

I have given the code that the rules are generated.I would like to create an item frequency histogram plot but ıt didn't work.
library(arules)
library(arulesViz)
library(rattle)
x <- read.table("C:/Users/toshıba pc/Desktop/Kitap2.csv", header = TRUE, sep = ";")
y <- as.matrix(x)
rules <- apriori(y, parameter = list(supp = 0.1, conf = 0.8))
itemFrequencyPlot(rules, support = 0.1, cex.names = 0.8)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘itemFrequencyPlot’ for signature ‘"rules"’
itemFrequencyPlot is not defined for rules. You can use
itemFrequencyPlot(items(rules))
to get the frequency of the items in the rules, but I am not sure that this will give you the results you want.

error with RBGL package in R

I want to install the RGBL package in bioconductor to perform some graph algorithms.
I updated R to the latest version 3.2.0, and installed the package as instructed on http://www.bioconductor.org/
source("http://bioconductor.org/biocLite.R")
biocLite("RBGL")
It was installed successfully, then I tried to run
library(graph)
library(RBGL)
x<- strongComp(graph)
and returns this error
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘isDirected’ for signature ‘"igraph"’
Here's the traceback
> traceback()
4: stop(gettextf("unable to find an inherited method for function %s for signature %s",
sQuote(fdef#generic), sQuote(cnames)), domain = NA)
3: (function (classes, fdef, mtable)
{
methods <- .findInheritedMethods(classes, fdef, mtable)
if (length(methods) == 1L)
return(methods[[1L]])
else if (length(methods) == 0L) {
cnames <- paste0("\"", vapply(classes, as.character,
""), "\"", collapse = ", ")
stop(gettextf("unable to find an inherited method for function %s for signature %s",
sQuote(fdef#generic), sQuote(cnames)), domain = NA)
}
else stop("Internal error in finding inherited methods; didn't return a unique method",
domain = NA)
})(list("igraph"), function (object)
standardGeneric("isDirected"), <environment>)
2: isDirected(g)
1: strongComp(graph)
My system is Windows 32-bit.
I'm not sure if this is enough information. Please let me know if any other information needed.
Any ideas are appreciated, thanks!
EDIT:
I used the igraph packaged to create the graph object from an edge list with weight
library(igraph)
graph<- graph.data.frame(edge.list[,c(2:4)],directed=TRUE)
I'm not very good with generating random number, here's a reproducible example for my graph
set.seed(123)
edge.list<-cbind(seq(10),c(1,1,2,3,3,4,5,5,5,5),c(2,2,3,5,4,3,4,4,4,2),
runif(10, 1, 30))
colnames(edge.list) <-c("ID","V1","V2","weight")
As pointed out in the comments, you need to make a graph object, not an igraph object.
Here's how I might transform your edge.list into the form that graph expects.
rawEL <- data.frame(source = as.character(edge.list[,1]),
edges = as.character(edge.list[,2]),
weights = edge.list[,3], stringsAsFactors=F
)
V <- unique(c(rawEL$source, rawEL$edges))
edL <- lapply(
split(
rawEL[,-1],
factor(edge.list[,1], levels=V)
), as.list
)
gr <- graphNEL(V, edL, "directed")
plot(gr)

Manhattan distance in R

I am going to calculate manhattan distance in R but I have a problem how to check if there exist a key in the hash.
My code is as follow. The error I am getting is in line with if statement.
library("hash")
h <- hash( list( Tom=list( Film1=4.0, Film2=1.0, Film3=4.0, Film4=4.0, Film5=1.0 ), Jon=list( Film1=3.0, Film2=5.0, Film3=4.0, Film4=2.5, Film5=3.0 ) ) )
manhattan_dist <- function(rating1, rating2){
distance <- 0
for(key in rating1){
if( has.key( key, rating2 ))
distance <- distance + (abs(rating1[[1]] - rating2[[1]]))
}
return(distance)
}
dist <- manhattan_dist(h$Tom, h$Jon)
dist
And the error is:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘has.key’ for signature ‘"numeric", "list"’
should work like this if you pass vector
manhattan_dist <- function(rating1, rating2){
distance <- abs(rating1-rating2)
distance <- sum(distance)
return(distance)
}

Resources