From kai.nitschke at uniklinik-freiburg.de  Thu Oct 22 18:59:28 2015
From: kai.nitschke at uniklinik-freiburg.de (Kai Nitschke)
Date: Thu, 22 Oct 2015 18:59:28 +0200
Subject: [Rd] C_LogLin (stats/loglin)
Message-ID: <20151022185928.npvro4f88co08kkk@webmail1.uniklinik-freiburg.de>

Hi everyone,

I have a question regarding a C function of the "stats" package in R.

I tried to understand the ?loglin? basic function of the ?stats?  
package implemented in
R. The implemented function itself runs without any problem (perhaps  
see sample). When I
ran it line by line it stopped at the lines 23-24 of the  
loglin-function; (the following line):
z <- .Call(C_LogLin, dtab, conf, table, start, nmar, eps, iter)

?.Call? calls the C/C++ function ?C_LogLin?. But when I am running it  
line by line I get
the following error on line 23/24:
Error: object 'C_LogLin' not found

Hence, my questions:
1. Where does the original ?loglin? function get the ?C_LogLin?  
function from (some libraries? where are they stored)?
2. Why does it not work in the normal R command (line by) line but in  
the implemented ?loglin? function?
3. Is it possible to isolate the ?C_LogLin? function so I can use it  
in other scripts or programming languages e.g. Matlab?

Thank you in advance for your help,
  Kai

--------------------------------------------
#Sample

# Example data
mytable = array(0,dim=c(3,4,5))
mytable[1,,1] = c(35,26,36,29); mytable[2,,1] = c(21,12,26,27);  
mytable[3,,1] = c(9,26,23,38);
mytable[1,,2] = c(43,26,89,26); mytable[2,,2] = c(78,42,32,9);  
mytable[3,,2] = c(34,39,42,51);
mytable[1,,3] = c(51,72,62,21); mytable[2,,3] = c(6,52,71,32);  
mytable[3,,3] = c(35,36,48,21);
mytable[1,,4] = c(12,26,53,42); mytable[2,,4] = c(96,75,81,56);  
mytable[3,,4] = c(32,65,75,63);
mytable[1,,5] = c(21,18,24,33); mytable[2,,5] = c(22,56,36,91);  
mytable[3,,5] = c(32,65,63,14);

# Call of the original function works fine
loglin(mytable,list(c(1,2),c(1,3)))

# line by line does not work
table = mytable; margin = list(c(1,2),c(1,3)); start = rep(1,  
length(table)); fit = FALSE; eps = 0.1;
iter = 20L; param = FALSE; print = TRUE;
rfit <- fit
dtab <- dim(table)
nvar <- length(dtab)
ncon <- length(margin)
conf <- matrix(0L, nrow = nvar, ncol = ncon)
nmar <- 0
varnames <- names(dimnames(table))
for (k in seq_along(margin)) {
         tmp <- margin[[k]]
         if (is.character(tmp)) {
             ## Rewrite margin names to numbers
             tmp <- match(tmp, varnames)
             margin[[k]] <- tmp
         }
         conf[seq_along(tmp), k] <- tmp
         nmar <- nmar + prod(dtab[tmp])
}
ntab <- length(table)
if (length(start) != ntab ) stop("'start' and 'table' must be same length")
z <- .Call(C_LogLin, dtab, conf, table, start, nmar, eps, iter)
...


-- 
Kai Nitschke, Dipl.-Psych.

Division of Functional Brain Imaging (FBI)
Dept. of Neurology, University of Freiburg

Breisacher Str. 64, 79095 Freiburg, Germany
Phone: +49 (0) 761 270 - 50410
Email: kai.nitschke at uniklinik-freiburg.de
URL: http://www.uniklinik-freiburg.de/fbi/people/kai-nitschke.html



[Rd] C_LogLin (stats/loglin)
Kai Nitschke