R Under development (unstable) (2024-09-12 r87143 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > source("RSquared.R") > > # rm(LiblineaR) > library(LiblineaR) > > set.seed(1) > N=10 > # Note that 1st record is negative > df=data.frame(x1 = (1:N)/N*10 + 2*rnorm(N), x2 = (1:N)/N*10 + 2*rnorm(N), x3 = (1:N)/N*10 + 2*rnorm(N)) > df$y.regr = apply(as.matrix(df),1,mean) + 2*rnorm(N) > df$y.logical = df$y.regr > 5.5 > df$y.int = ifelse(df$y.logical, 1L, -1L) > df$y.double = as.double(df$y.int) > df$y.char = as.character(df$y.int) > df$y.factor = factor(df$y.int) > df$y.factorRev = factor(df$y.int, levels=rev(levels(df$y.factor))) > df$y.factorExtra = factor(df$y.int, levels=c(-1,1,99), labels=c("no","yes","maybe")) > df$y.multiclass = cut(df$y.regr, breaks=c(-99,4,7,99)) > # Expected: good classification performance, negative bias and positive sum of weights > > regrTargets = "y.regr" > classifTargets = setdiff(grep("^y",colnames(df), value=TRUE), regrTargets) > binTargets = setdiff(classifTargets,"y.multiclass") > > testClassif = function(rev,yy,weighted,tt) { + cat("Testing",rev,yy,weighted,tt,"\n") + if(rev) + is=1:nrow(df) + else + is=nrow(df):1 + nis = which(!df[is,"y.logical"]) + + if(weighted) # class weights giving more importance to negative classes + wi=c("1"=2,"TRUE"=2,"yes"=2, "(7,99]"=1, + "(4,7]"=50, + "-1"=100,"FALSE"=100,"no"=100,"(-99,4]"=150) + else + wi=NULL + + y = df[is,yy] + x = df[is,1:3] + m = LiblineaR(x, y, type = tt, wi=wi) + p = predict(m, newx = x) + res=c( + type=tt, + target=yy, + weighted=weighted, + y1 = as.character(y[1]), + perf=(mean(as.character(y)==as.character(p$predictions))), # >= 0.75 + perfNeg=(mean(as.character(y[nis])==as.character(p$predictions[nis]))), # >= 0.75 + dimW=paste(dim(m$W), collapse = " "), + sumW=sum(m$W[,1:3]), + biasW=m$W[1,][["Bias"]], + classNames=paste(m$ClassNames, collapse = " "), + yLev=paste(levels(y), collapse=" "), + predLev=paste(levels(p$predictions), collapse=" "), + yClass=class(y), + predClass=class(p$predictions), + weights=paste(colnames(m$W),"=",round(m$W[1,],3),collapse = " ; ") + ) + + #table(true=as.character(y),pred=as.character(p$predictions)) + + return(res) + } > > testRegr = function(rev,yy,tt) { + cat("Testing",rev,yy,tt,"\n") + + if(rev) + is=1:nrow(df) + else + is=nrow(df):1 + + y = df[is,yy] + x = df[is,1:3] + m = LiblineaR(x, y, type = tt, svr_eps=.1) + p = predict(m, newx = x) + res=c( + type=tt, + target=yy, + weighted=FALSE, + y1 = as.character(y[1]), + perf=RSquared(p$predictions, y), # >= 0.75 + perfNeg=0, + dimW=paste(dim(m$W), collapse = " "), + sumW=sum(m$W[,1:3]), + biasW=m$W[1,][["Bias"]], + classNames=paste(m$ClassNames, collapse = " "), + yLev=paste(levels(y), collapse=" "), + predLev=paste(levels(p$predictions), collapse=" "), + yClass=class(y), + predClass=class(p$predictions), + weights=paste(colnames(m$W),"=",round(m$W[1,],3),collapse = " ; ") + ) + + return(res) + } > > > allRes=NULL > for(tt in 0:7) { + for(weighted in c(FALSE,TRUE)) { + for(rev in c(FALSE,TRUE)) { + for (yy in classifTargets) { + res = testClassif(rev,yy,weighted,tt) + allRes=rbind(allRes,res) + } + } + } + } Testing FALSE y.logical FALSE 0 Testing FALSE y.int FALSE 0 Testing FALSE y.double FALSE 0 Testing FALSE y.char FALSE 0 Testing FALSE y.factor FALSE 0 Testing FALSE y.factorRev FALSE 0 Testing FALSE y.factorExtra FALSE 0 Testing FALSE y.multiclass FALSE 0 Testing TRUE y.logical FALSE 0 Testing TRUE y.int FALSE 0 Testing TRUE y.double FALSE 0 Testing TRUE y.char FALSE 0 Testing TRUE y.factor FALSE 0 Testing TRUE y.factorRev FALSE 0 Testing TRUE y.factorExtra FALSE 0 Testing TRUE y.multiclass FALSE 0 Testing FALSE y.logical TRUE 0 Testing FALSE y.int TRUE 0 Testing FALSE y.double TRUE 0 Testing FALSE y.char TRUE 0 Testing FALSE y.factor TRUE 0 Testing FALSE y.factorRev TRUE 0 Testing FALSE y.factorExtra TRUE 0 Testing FALSE y.multiclass TRUE 0 Testing TRUE y.logical TRUE 0 Testing TRUE y.int TRUE 0 Testing TRUE y.double TRUE 0 Testing TRUE y.char TRUE 0 Testing TRUE y.factor TRUE 0 Testing TRUE y.factorRev TRUE 0 Testing TRUE y.factorExtra TRUE 0 Testing TRUE y.multiclass TRUE 0 Testing FALSE y.logical FALSE 1 Testing FALSE y.int FALSE 1 Testing FALSE y.double FALSE 1 Testing FALSE y.char FALSE 1 Testing FALSE y.factor FALSE 1 Testing FALSE y.factorRev FALSE 1 Testing FALSE y.factorExtra FALSE 1 Testing FALSE y.multiclass FALSE 1 Testing TRUE y.logical FALSE 1 Testing TRUE y.int FALSE 1 Testing TRUE y.double FALSE 1 Testing TRUE y.char FALSE 1 Testing TRUE y.factor FALSE 1 Testing TRUE y.factorRev FALSE 1 Testing TRUE y.factorExtra FALSE 1 Testing TRUE y.multiclass FALSE 1 Testing FALSE y.logical TRUE 1 Testing FALSE y.int TRUE 1 Testing FALSE y.double TRUE 1 Testing FALSE y.char TRUE 1 Testing FALSE y.factor TRUE 1 Testing FALSE y.factorRev TRUE 1 Testing FALSE y.factorExtra TRUE 1 Testing FALSE y.multiclass TRUE 1 Testing TRUE y.logical TRUE 1 Testing TRUE y.int TRUE 1 Testing TRUE y.double TRUE 1 Testing TRUE y.char TRUE 1 Testing TRUE y.factor TRUE 1 Testing TRUE y.factorRev TRUE 1 Testing TRUE y.factorExtra TRUE 1 Testing TRUE y.multiclass TRUE 1 Testing FALSE y.logical FALSE 2 Testing FALSE y.int FALSE 2 Testing FALSE y.double FALSE 2 Testing FALSE y.char FALSE 2 Testing FALSE y.factor FALSE 2 Testing FALSE y.factorRev FALSE 2 Testing FALSE y.factorExtra FALSE 2 Testing FALSE y.multiclass FALSE 2 Testing TRUE y.logical FALSE 2 Testing TRUE y.int FALSE 2 Testing TRUE y.double FALSE 2 Testing TRUE y.char FALSE 2 Testing TRUE y.factor FALSE 2 Testing TRUE y.factorRev FALSE 2 Testing TRUE y.factorExtra FALSE 2 Testing TRUE y.multiclass FALSE 2 Testing FALSE y.logical TRUE 2 Testing FALSE y.int TRUE 2 Testing FALSE y.double TRUE 2 Testing FALSE y.char TRUE 2 Testing FALSE y.factor TRUE 2 Testing FALSE y.factorRev TRUE 2 Testing FALSE y.factorExtra TRUE 2 Testing FALSE y.multiclass TRUE 2 Testing TRUE y.logical TRUE 2 Testing TRUE y.int TRUE 2 Testing TRUE y.double TRUE 2 Testing TRUE y.char TRUE 2 Testing TRUE y.factor TRUE 2 Testing TRUE y.factorRev TRUE 2 Testing TRUE y.factorExtra TRUE 2 Testing TRUE y.multiclass TRUE 2 Testing FALSE y.logical FALSE 3 Testing FALSE y.int FALSE 3 Testing FALSE y.double FALSE 3 Testing FALSE y.char FALSE 3 Testing FALSE y.factor FALSE 3 Testing FALSE y.factorRev FALSE 3 Testing FALSE y.factorExtra FALSE 3 Testing FALSE y.multiclass FALSE 3 Testing TRUE y.logical FALSE 3 Testing TRUE y.int FALSE 3 Testing TRUE y.double FALSE 3 Testing TRUE y.char FALSE 3 Testing TRUE y.factor FALSE 3 Testing TRUE y.factorRev FALSE 3 Testing TRUE y.factorExtra FALSE 3 Testing TRUE y.multiclass FALSE 3 Testing FALSE y.logical TRUE 3 Testing FALSE y.int TRUE 3 Testing FALSE y.double TRUE 3 Testing FALSE y.char TRUE 3 Testing FALSE y.factor TRUE 3 Testing FALSE y.factorRev TRUE 3 Testing FALSE y.factorExtra TRUE 3 Testing FALSE y.multiclass TRUE 3 Testing TRUE y.logical TRUE 3 Testing TRUE y.int TRUE 3 Testing TRUE y.double TRUE 3 Testing TRUE y.char TRUE 3 Testing TRUE y.factor TRUE 3 Testing TRUE y.factorRev TRUE 3 Testing TRUE y.factorExtra TRUE 3 Testing TRUE y.multiclass TRUE 3 Testing FALSE y.logical FALSE 4 Testing FALSE y.int FALSE 4 Testing FALSE y.double FALSE 4 Testing FALSE y.char FALSE 4 Testing FALSE y.factor FALSE 4 Testing FALSE y.factorRev FALSE 4 Testing FALSE y.factorExtra FALSE 4 Testing FALSE y.multiclass FALSE 4 Testing TRUE y.logical FALSE 4 Testing TRUE y.int FALSE 4 Testing TRUE y.double FALSE 4 Testing TRUE y.char FALSE 4 Testing TRUE y.factor FALSE 4 Testing TRUE y.factorRev FALSE 4 Testing TRUE y.factorExtra FALSE 4 Testing TRUE y.multiclass FALSE 4 Testing FALSE y.logical TRUE 4 Testing FALSE y.int TRUE 4 Testing FALSE y.double TRUE 4 Testing FALSE y.char TRUE 4 Testing FALSE y.factor TRUE 4 Testing FALSE y.factorRev TRUE 4 Testing FALSE y.factorExtra TRUE 4 Testing FALSE y.multiclass TRUE 4 Testing TRUE y.logical TRUE 4 Testing TRUE y.int TRUE 4 Testing TRUE y.double TRUE 4 Testing TRUE y.char TRUE 4 Testing TRUE y.factor TRUE 4 Testing TRUE y.factorRev TRUE 4 Testing TRUE y.factorExtra TRUE 4 Testing TRUE y.multiclass TRUE 4 Testing FALSE y.logical FALSE 5 Testing FALSE y.int FALSE 5 Testing FALSE y.double FALSE 5 Testing FALSE y.char FALSE 5 Testing FALSE y.factor FALSE 5 Testing FALSE y.factorRev FALSE 5 Testing FALSE y.factorExtra FALSE 5 Testing FALSE y.multiclass FALSE 5 Testing TRUE y.logical FALSE 5 Testing TRUE y.int FALSE 5 Testing TRUE y.double FALSE 5 Testing TRUE y.char FALSE 5 Testing TRUE y.factor FALSE 5 Testing TRUE y.factorRev FALSE 5 Testing TRUE y.factorExtra FALSE 5 Testing TRUE y.multiclass FALSE 5 Testing FALSE y.logical TRUE 5 Testing FALSE y.int TRUE 5 Testing FALSE y.double TRUE 5 Testing FALSE y.char TRUE 5 Testing FALSE y.factor TRUE 5 Testing FALSE y.factorRev TRUE 5 Testing FALSE y.factorExtra TRUE 5 Testing FALSE y.multiclass TRUE 5 Testing TRUE y.logical TRUE 5 Testing TRUE y.int TRUE 5 Testing TRUE y.double TRUE 5 Testing TRUE y.char TRUE 5 Testing TRUE y.factor TRUE 5 Testing TRUE y.factorRev TRUE 5 Testing TRUE y.factorExtra TRUE 5 Testing TRUE y.multiclass TRUE 5 Testing FALSE y.logical FALSE 6 Testing FALSE y.int FALSE 6 Testing FALSE y.double FALSE 6 Testing FALSE y.char FALSE 6 Testing FALSE y.factor FALSE 6 Testing FALSE y.factorRev FALSE 6 Testing FALSE y.factorExtra FALSE 6 Testing FALSE y.multiclass FALSE 6 Testing TRUE y.logical FALSE 6 Testing TRUE y.int FALSE 6 Testing TRUE y.double FALSE 6 Testing TRUE y.char FALSE 6 Testing TRUE y.factor FALSE 6 Testing TRUE y.factorRev FALSE 6 Testing TRUE y.factorExtra FALSE 6 Testing TRUE y.multiclass FALSE 6 Testing FALSE y.logical TRUE 6 Testing FALSE y.int TRUE 6 Testing FALSE y.double TRUE 6 Testing FALSE y.char TRUE 6 Testing FALSE y.factor TRUE 6 Testing FALSE y.factorRev TRUE 6 Testing FALSE y.factorExtra TRUE 6 Testing FALSE y.multiclass TRUE 6 Testing TRUE y.logical TRUE 6 Testing TRUE y.int TRUE 6 Testing TRUE y.double TRUE 6 Testing TRUE y.char TRUE 6 Testing TRUE y.factor TRUE 6 Testing TRUE y.factorRev TRUE 6 Testing TRUE y.factorExtra TRUE 6 Testing TRUE y.multiclass TRUE 6 Testing FALSE y.logical FALSE 7 Testing FALSE y.int FALSE 7 Testing FALSE y.double FALSE 7 Testing FALSE y.char FALSE 7 Testing FALSE y.factor FALSE 7 Testing FALSE y.factorRev FALSE 7 Testing FALSE y.factorExtra FALSE 7 Testing FALSE y.multiclass FALSE 7 Testing TRUE y.logical FALSE 7 Testing TRUE y.int FALSE 7 Testing TRUE y.double FALSE 7 Testing TRUE y.char FALSE 7 Testing TRUE y.factor FALSE 7 Testing TRUE y.factorRev FALSE 7 Testing TRUE y.factorExtra FALSE 7 Testing TRUE y.multiclass FALSE 7 Testing FALSE y.logical TRUE 7 Testing FALSE y.int TRUE 7 Testing FALSE y.double TRUE 7 Testing FALSE y.char TRUE 7 Testing FALSE y.factor TRUE 7 Testing FALSE y.factorRev TRUE 7 Testing FALSE y.factorExtra TRUE 7 Testing FALSE y.multiclass TRUE 7 Testing TRUE y.logical TRUE 7 Testing TRUE y.int TRUE 7 Testing TRUE y.double TRUE 7 Testing TRUE y.char TRUE 7 Testing TRUE y.factor TRUE 7 Testing TRUE y.factorRev TRUE 7 Testing TRUE y.factorExtra TRUE 7 Testing TRUE y.multiclass TRUE 7 > > for(tt in 11:13) { + for(rev in c(FALSE,TRUE)) { + for (yy in regrTargets) { + res = testRegr(rev,yy,tt) + allRes=rbind(allRes,res) + } + } + } Testing FALSE y.regr 11 Testing TRUE y.regr 11 Testing FALSE y.regr 12 Testing TRUE y.regr 12 Testing FALSE y.regr 13 Testing TRUE y.regr 13 > > allRes = as.data.frame(allRes,stringsAsFactors = F) > # Simple tests > allRes$dimOK=(allRes$type=="4" | allRes$target=="y.multiclass" | allRes$dimW=="1 4") > allRes$perfOK=(allRes$target=="y.multiclass" & allRes$perf>=.6 | + ifelse(allRes$weighted, allRes$perfNeg>=.9, allRes$perf>=.75)) > allRes$sumOK=(!allRes$target%in%c("y.int","y.double") | allRes$type=="4" | as.numeric(allRes$sumW)>0) > allRes$biasOK=(!allRes$target%in%c("y.int","y.double") | allRes$type=="4" | as.numeric(allRes$biasW)<0) > allRes$levelsOK=(allRes$target%in%c("y.char","y.double") | (allRes$yLev==allRes$predLev & allRes$yClass==allRes$predClass)) > > # View(allRes) > # library(reshape2) > # print(dcast(allRes, dimOK + perfOK + levelsOK + sumOK + biasOK ~ .)) > > #=============================================================================== > # Testing sparse matrices of various formats > > # Sparsifying the iris dataset: > iS=apply(iris[,1:4],2,function(a){a[a y=iris[,5] > > # Sparse matrix of class matrix.csr, matrix.csc, matrix.coo from SparseM package : > if(require(SparseM)){ + cat("Testing sparse matrices of class matrix.csr from package SparseM.\n") + irisSparse<-as.matrix.csr(iS) + acc=LiblineaR(data=irisSparse,target=y,type=0,cost=0.1,bias=1,verbose=FALSE,cross = 5) + cat("Accuracy:",acc,"\n",sep=" ") + + cat("Testing sparse matrices of class matrix.csc from package SparseM.\n") + irisSparse<-as.matrix.csc(iS) + acc=LiblineaR(data=irisSparse,target=y,type=0,cost=0.1,bias=1,verbose=FALSE,cross = 5) + cat("Accuracy:",acc,"\n",sep=" ") + + cat("Testing sparse matrices of class matrix.coo from package SparseM.\n") + irisSparse<-as.matrix.coo(iS) + acc=LiblineaR(data=irisSparse,target=y,type=0,cost=0.1,bias=1,verbose=FALSE,cross = 5) + cat("Accuracy:",acc,"\n",sep=" ") + + } Loading required package: SparseM Testing sparse matrices of class matrix.csr from package SparseM. Accuracy: 0.78 Testing sparse matrices of class matrix.csc from package SparseM. Accuracy: 0.7333333 Testing sparse matrices of class matrix.coo from package SparseM. Accuracy: 0.74 > > # Sparse matrix of class dgRMatrix, dgCMatrix, dgTMatrix from Matrix package : > if(require(Matrix)){ + cat("Testing sparse matrices of class dgCMatrix from package Matrix.\n") + irisSparse<-as(iS,"sparseMatrix") + acc=LiblineaR(data=irisSparse,target=y,type=0,cost=0.1,bias=1,verbose=FALSE,cross=5) + cat("Accuracy:",acc,"\n",sep=" ") + + cat("Testing sparse matrices of class dgRMatrix from package Matrix.\n") + irisSparse<-as(iS,"sparseMatrix") + irisSparse<-as(as(irisSparse,"matrix"),"dgRMatrix") + acc=LiblineaR(data=irisSparse,target=y,type=0,cost=0.1,bias=1,verbose=FALSE,cross=5) + cat("Accuracy:",acc,"\n",sep=" ") + + cat("Testing sparse matrices of class dgTMatrix from package Matrix.\n") + irisSparse<-as(iS,"sparseMatrix") + irisSparse<-as(as(irisSparse,"matrix"),"dgTMatrix") + acc=LiblineaR(data=irisSparse,target=y,type=0,cost=0.1,bias=1,verbose=FALSE,cross=5) + cat("Accuracy:",acc,"\n",sep=" ") + + } Loading required package: Matrix Attaching package: 'Matrix' The following object is masked from 'package:SparseM': det Testing sparse matrices of class dgCMatrix from package Matrix. 'as(, "dgRMatrix")' is deprecated. Use 'as(as(as(., "dMatrix"), "generalMatrix"), "RsparseMatrix")' instead. See help("Deprecated") and help("Matrix-deprecated"). Accuracy: 0.7666667 Testing sparse matrices of class dgRMatrix from package Matrix. Accuracy: 0.8 Testing sparse matrices of class dgTMatrix from package Matrix. Accuracy: 0.8 > #' > #' ############################################# > > proc.time() user system elapsed 1.82 0.17 2.00