exponential matrix function definition? - r

I am running a machine that only has R 2.10 and cannot be updated (this is a fact: it cannot be changed).
I need to use the "expm" package's functionality for taking the kth power of a matrix (it is the function %^%) but I cannot figure out how to copy the source code, since the earliest version of this package was made for versions later than R 2.10. On R, after installing the expm package, running:
library(expm)
getAnywhere('%^%')
gives me the source code, but then when I copy this source code in the form %^% <- *source code*, I get the error
Error in stopifnot(is.numeric(x) || is(x, "dMatrix"), length(d <- dim(x)) == :
object 'x' not found
Can anyone help me figure out how to use the original implementation of %^% via source code?

Use backticks ` to denote a non-syntactic name.
`%^%` <- ....

Related

Walsh-Hadamard Transform in r

I search for a command to compute Walsh-Hadamard Transform of an image in R, but I don't find anything. In MATLAB fwht use for this. this command implement Walsh-Hadamard Tranform to each row of matrix. Can anyone introduce a similar way to compute Walsh-Hadamard on rows or columns of Matrix in R?
I find a package here:
http://www2.uaem.mx/r-mirror/web/packages/boolfun/boolfun.pdf
But why this package is not available when I want to install it?
Packages that are not maintained get put in the Archive. They get put there when that aren't updated to match changing requirements or start making errors with changing R code base. https://cran.r-project.org/web/packages/boolfun/index.html
It's possible that you might be able to extract useful code from the archive version, despite the relatively ancient version of R that package was written under.
The R code for walshTransform calls an object code routine:
walshTransform <- function ( truthTable ) # /!\ should check truthTable values are in {0,1}
{
len <- log(length(truthTable),base=2)
if( len != round(len) )
stop("bad truth table length")
res <- .Call( "walshTransform",
as.integer(truthTable),
as.integer(len))
res
}
Installing the package succeeded on my Mac, but would require the appropriate toolchain on whatever OS you are working in.

mas5 normalization error: unable to find an inherited method for function

Goal: mas5 normalize data.
Problem: when I try the following R code, I get this
error: unable to find an inherited method for function bg.correct for signature ExpressionFeatureSet, character
I have looked on SO, and found the following: What does this mean: unable to find an inherited method for function ‘A’ for signature ‘"B"’, but I am not exactly sure how to fix my specific problem and use the mas5 function properly. I have also looked at this affy manual but still stuck...
installpkg("affy")
library('affy')
setwd("/Users/er/Desktop/DesktopFolders/DataSets/CD8Helios/Microarray/CELfiles/CEL")
cel_Files <- list.celfiles()
affyRaw <- read.celfiles(cel_Files)
eset <- mas5(affyRaw)
If you are sure that the .cel files were created based on experiments performed on the type of array that works with affy package than you should try this workflow using ReadAffy from affy package.
cel_Files <- list.celfiles()
affyRaw <- affy::ReadAffy(filenames=cel_Files)
eset <- mas5(affyRaw)
However, it might be the case that the affy package is not designed for your array type. Then, you should switch to the oligo and oligoClasses packages and normalize with analogous function rma
cel_Files <- oligoClasses::list.celfiles()
affyRaw <- oligo::read.celfiles(cel_Files)
eset <- oligo::rma(affyRaw)

extracting source code from r package

I am trying to install the r package sowas and unfortunately it is too old to implement in the new versions of r.
According to the author you can use the package using the source() function to gain access to the code but I have not been able to figure out how to do that.
Any help is appreciated.
Here is a link to the package I described as it is not a CRAN package: http://tocsy.pik-potsdam.de/wavelets/
The .zip file is a windows binary and as such it won't be too interesting. What you'll want to look at is the contents of the .tar.gz archive. You can extract those contents and then look at the code in the R subdirectory.
You could also update the package to work with new versions of R so that you can actually build and install the package. To do so you could unpack the .tar.gz as before but now you'll need to add a NAMESPACE file. This is just a plaintext file at the top of the package directory that has a form like:
export(createar)
export(createwgn)
export(criticalvaluesWCO)
export(criticalvaluesWSP)
export(cwt.ts)
export(plot.wt)
export(plotwt)
export(readmatrix)
export(readts)
export(rk)
export(wco)
export(wcs)
export(writematrix)
export(wsp)
Where you have an export statement for any function in the package that you actually want to be able to use. If a function isn't exported then the functions in the package still have access to that function but the user can't use it (as easily). Once you do that you should be able to build and install the package.
I took the liberty of doing some of this already. I haven't actually taken the time to figure out which functions are useful and should be exported and just assumed that if a help page was written for the function that it should be exported and if there wasn't a help page then I didn't export it. I used Rd2roxygen to convert the help pages to roxygen code (because that's how I roll) and had to do a little bit of cleanup after that but it seems to install just fine.
So if you have the devtools package installed you should actually be able to install the version I modified directly by using the following commands
library(devtools)
install_github("SOWAS", "Dasonk")
Personally I would recommend that you go the route of adding the NAMESPACE file and what not directly as then you'll have more control over the code and be more able to fix any problems that might occur when using the package. Or if you use git you could fork my repo and continue fixing things from there. Good luck.
If you want to see the source code of a particular function, then just type the name of the function without the braces and press enter. You will see the code.
For example type var in command prompt to see it's code.
> var
function (x, y = NULL, na.rm = FALSE, use)
{
if (missing(use))
use <- if (na.rm)
"na.or.complete"
else "everything"
na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs",
"everything", "na.or.complete"))
if (is.na(na.method))
stop("invalid 'use' argument")
if (is.data.frame(x))
x <- as.matrix(x)
else stopifnot(is.atomic(x))
if (is.data.frame(y))
y <- as.matrix(y)
else stopifnot(is.atomic(y))
.Call(C_cov, x, y, na.method, FALSE)
}
<bytecode: 0x0000000008c97980>
<environment: namespace:stats>

N Choose K function in R not working--what am I missing?

I was trying to acquaint myself with R's nChooseK function but I can't get it to work. I thought it was part of the standard setup (i.e. no additional package needed).
Please help. Here is what I tried:
> nChooseK(10,2)
Error: could not find function "nChooseK"
> n<-4;k<-2
> print(nChooseK(n,k))
Error in print(nChooseK(n, k)) : could not find function "nChooseK"
the last one was an example I saw here: R basic nChooseK
The function is in the R.basic package which is not part of the default R installation. You probably meant to use just choose().
As joran mentions the function nChooseK is a part of R.basic. You can tell this from the example you posted by looking at the top of the page:
You'll notice the "R.basic" in the curley braces which tells you that that function is a part of the "R.basic" package. So to use nChooseK you'll first need to load that package
library(R.basic)
If you don't have R.basic installed yet then you'll need to install it
install.packages("R.basic", contriburl="http://www.braju.com/R/repos/")
library(R.basic)
But as noted the choose function in base R does the same thing
choose(37, 12)
#[1] 1852482996
nChooseK(37, 12)
#[1] 1852482996

How to correct for "Error in nullmodel(comm, method) : could not find function "list2env" in the vegan package

I'm busy exploring the package vegan for R, using it to calculate nestedness of community matrices and null models. I'm particularly interested in using the permat functions as well as Oecosimu.
However, when running my program I obtained the following errors:
Error in nullmodel(comm, method) : could not find function "list2env"
Error in nullmodel(m, ALGO) : could not find function "list2env"
I then even ran an example (given below) of how to use these functions given by the R help function, and even these examples gave the same error. Am I suppose to import something else in order to use these functions or how do I go about fixing this?
Examples:
m <- matrix(c(
1,3,2,0,3,1,
0,2,1,0,2,1,
0,0,1,2,0,3,
0,0,0,1,4,3
), 4, 6, byrow=TRUE)
x1 <- permatswap(m, "quasiswap")
summary(x1)
x2 <- permatfull(m)
summary(x2)
x3 <- permatfull(m, "none", mtype="prab")
x3$orig
summary(x3)
x4 <- permatfull(m, strata=c(1,1,2,2))
summary(x4)
Technically, this is a bug in the development version of Vegan on R-Forge. We were failing to declare a dependency on R versions >= 2.12 in DESCRIPTION. I have checked in the relevant change to the source tree to fix this but it will take a day or so before the tarball and binaries are rebuilt by R-Forge.
That said, you should probably update your R to something more recent. Or use the versions of those functions provided in Vegan 2.0-x on CRAN.
list2env is part of R base, which means it comes with the distribution, not in an add-on package. So if you don't have it you're probably either running an old version of R or have a broken installation. The example worked fine for me, with R 2.12.1 and vegan 2.1-0.
Your code works for me without an error message
The most probable cause of your error is your using old versions of R, vegan or permute
The R news for changes says
CHANGES IN R VERSION 2.12.0: NEW FEATURES:
o New list2env() utility function as an inverse of
as.list(<environment>) and for fast multi-assign() to existing
environment. as.environment() is now generic and uses list2env()
as list method.
CHANGES IN R VERSION 2.12.1: BUG FIXES:
o When list2env() created an environment it was missing a PROTECT
call and so was vulnerable to garbage collection.
CHANGES IN R VERSION 2.13.0: NEW FEATURES:
o list2env(envir = NULL) defaults to hashing (with a suitably sized
environment) for lists of more than 100 elements.
So update your version of R and the packages and try again.

Resources