Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Friday, April 25, 2014

capture.output function in R

Once I was wondering how to return string from function str().  I guess capture.output is one way:

capture.output              package:utils              R Documentation

Send Output to a Character String or File

Description:

     Evaluates its arguments with the output being returned as a
     character string or sent to a file.  Related to 'sink' in the same
     way that 'with' is related to 'attach'.

Usage:

     capture.output(..., file = NULL, append = FALSE)

Arguments:

     ...: Expressions to be evaluated.

Thursday, February 21, 2013

create function documentation template in R

create function documentation template in R

 prompt(monitor, file = 'monitor.Rd', force.function = TRUE)

Saturday, September 22, 2012

r profile.site change options permanently

It seems the following link points to an approach, however, it does not work well. 


So at least this file can be changed for this purpose, 

library/base/R/Rprofile 

Saturday, July 21, 2012

take a look at the code of a s4 method

getMethod('sampling', signature='s4class')

Sunday, July 15, 2012

R machine related limits & numbers

?.Machines 
sample.int(.Machine$integer.max, 1)

Friday, July 13, 2012

R's memory limits

?"Memory-limits"

Sunday, May 20, 2012

list 2 env in R

list2env {base}R Documentation

From A List, Build or Add To an Environment

Description

From a named list x, create an environment containing all list components as objects, or "multi-assign" from x into a pre-existing environment.

Thursday, February 23, 2012

create variables in a created environment inside global environment inside a function

# create an environment inside the global 
# environment and create variables inside
# that environment 

# ref: http://goo.gl/GCQhZ
foofun <- function() {
   assign("env1", new.env(parent = globalenv()),  envir = globalenv()) 

   # this would not work 
   # env1 <- new.env(parent = globalenv());
   assign("tmpv", "tmpv", envir = globalenv()$env1) 
} 

foofun() 

ls() 

ls(envir = globalenv()$env1) 

rm("env1", envir = globalenv()) 
ls() 

#

Friday, February 17, 2012

lme4 nested example

http://lme4.r-forge.r-project.org/book/
chapter 2, we have example of nested factors

cask is nested in batch

so
strength ~ 1 + (1 | cask) + (1 | batch)
is problematic,

the correct one is
strength ~ 1 + (1 | cask) + (1 | batch:cask)
though
it is equivalent to
strength ~ 1 + (1 | batch / cask).

Saturday, February 11, 2012

symbolic description of factorial models for analysis of variance

Symbolic Description of Factorial Models for Analysis of Variance

G. N. Wilkinson and C. E. Rogers


Y_ij = m + a_i + b_j + (ab)_ij

A + B + A : B

Y_ij = m + a_i + (ab)_ij

A + A : B

---
A * B = A + B + A : B
A / B = A + A : B

blocks / plots 
nitrate * density 

Thursday, February 9, 2012

R code to fit varying intercepts with nested structure linear model


## sim some data and fit an varying intercept model
##

a <- 1:3
b <- 1:4

intercepts <- rnorm(12) 
intnames <- outer(a, b, FUN = function(x, y) { paste(x, y, sep = ':')} )
names(intercepts) <- as.vector(t(intnames)) 

N <- 10000; 
x <- round(abs(rnorm(N)) * 10, 2)
noise <- rnorm(N); 
ai <- sample(a, N, replace = TRUE) 
bi <- sample(b, N, replace = TRUE) 
beta <- 4; 
y <- intercepts[ai * 4 + bi - 4] + x * beta + noise 

ai <- as.factor(ai)
bi <- as.factor(bi)
table(ai:bi) 
print(intercepts) 
# varying intercepts model specification
fita <- lm(y ~ -1 + ai : bi + x) 
# in R lm and lmer's model specification, A * B = A + B + A : B
# where A : B is the interaction of A and B. So in the above,
# ai : bi is used to specify varying intercepts and -1 to
# get rid of the global intercept. 

Saturday, January 28, 2012

R draw ellipse

 1
 2 N <- 100;
 3 x <- numeric(N);
 4 y <- numeric(N);
 5 x[1] <- -100;
 6 y[1] <- -100;
 7 for (i in 2:N) {
 8   x[i] <- rnorm(1, 2 + (y[i - 1] - 3) / 6, sqrt(115 / 3))
 9   y[i] <- rnorm(1, 3 + (x[i] - 2) / 4, sqrt(115 / 2))
10 }
11 library(mixtools)
12 plot(y ~ x);
13 mu <- c(2, 3);
14 Sigma <- matrix(c(40, 10, 10, 60), ncol = 2)
15 ellipse(mu, Sigma);
16 

Thursday, January 12, 2012

matrix to vector by row of by col in R

m <- matrix(....) 
as.vector(m)   # by column 
as.vector(t(m)) # by row 

Friday, September 16, 2011

rjags on ubuntu -- nonzero exit status

on ubuntu 10.10 and rjags 3.x, I could still have this problem.

On 05/27/2010 05:23 AM, Scott Brogan wrote:
> I've not been able to install rjags successfully in Ubuntu 10.04 :
>
> ** testing if installed package can be loaded
> Error : .onLoad failed in loadNamespace() for 'rjags', details:
> call: dyn.load(file, DLLpath = DLLpath, ...)
> error: unable to load shared library
> '/home/scott/R/i486-pc-linux-gnu-library/2.11/rjags/libs/rjags.so':
> libjags.so.2: cannot open shared object file: No such file or directory
> ERROR: loading failed
>
> I've tried with different versions of both JAGS& rjags; lastly with R
> 2.11.0, JAGS 2.1.0,& rjags_2.1.0-4. Could anyone help ?
>

Scott,

I also had trouble installing rjags in 10.04. I got the same error you
did, even though I could see the .so files. The fix (I think, I tried
lots of things) was to run 'ldconfig', then launch R and install the
package. Apparently, my jags install did not update the libraries.

Hope this helps,
Michael

Wednesday, September 14, 2011

old ubuntu (maverick), new R

add the cran apt source repository to /etc/apt/sources.list
see
http://cran.r-project.org/bin/linux/ubuntu/README.html

in which there is a part about security. 



Sunday, July 3, 2011

R mathplot plotmath

it is ?plotmath to get the help in R. 

You cannot imagine that at some time point, you would forget this. 

Friday, July 1, 2011

Vim file type detection: only R for .r and .R

copied from 
http://biostat.mc.vanderbilt.edu/wiki/Main/RVim

Filetype detection

Vim includes syntax highlighting for two other languages that use the .r and .R file extensions. Vim searches up to the first 50 lines of a file and checks the comment syntax to determine whether it is R or Rexx. If no comments are found, it defaults to Rexx. This can be annoying for starting a new script. If you want .r and .R to ALWAYS be associated with R syntax, put au BufNewFile,BufRead .r,.R  setf r in your ~/.vim/filetype.vim (alternatively, you can use the same line in~/.vim/ftdetect/r.vim).

=====

Under ubuntu maverick, I would just change file /usr/share/vim/vim72/filetype.vim