From kai.nitschke at uniklinik-freiburg.de  Fri Oct 23 08:02:08 2015
From: kai.nitschke at uniklinik-freiburg.de (Kai Nitschke)
Date: Fri, 23 Oct 2015 08:02:08 +0200
Subject: [Rd] C_LogLin (stats/loglin)
In-Reply-To: <CADwqtCPBwwUitThLStq6WoVJcmjEpHSqByrqgmT7WuFbPsBAJg@mail.gmail.com>
References: <20151022185928.npvro4f88co08kkk@webmail1.uniklinik-freiburg.de>
	<CADwqtCPBwwUitThLStq6WoVJcmjEpHSqByrqgmT7WuFbPsBAJg@mail.gmail.com>
Message-ID: <20151023080208.xuxt4vpuxw4csog0@webmail1.uniklinik-freiburg.de>

Hi Gabriel,

it actually works with: z <- .Call(stats:::C_LogLin, dtab, conf,  
table, start, nmar, eps, iter).

This helps me a great deal. I will inform myself further about SEXPs.

Thank you very much,
  Kai

Zitat von Gabriel Becker <gmbecker at ucdavis.edu>:

> Kai,
>
> Apologies for the double message, it didn't go to the list last time.
>
> On Thu, Oct 22, 2015 at 9:59 AM, Kai Nitschke <
> kai.nitschke at uniklinik-freiburg.de> wrote:
>
>>
>> ?.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)?
>>
>
> The libraries are in the stats package, built from the c code found in the
> /src  directory of the package's source code.
>
>
>> 2. Why does it not work in the normal R command (line by) line but in the
>> implemented ?loglin? function?
>>
>
> Notice that C_LogLin is not in quotes. That is actually an R object, not a
> string, and it is one that is not exported by the stats package.
> stats:::C_LogLin would probably work.
>
>
>> 3. Is it possible to isolate the ?C_LogLin? function so I can use it in
>> other scripts or programming languages e.g. Matlab?
>>
>
>
> A function called by the .Call interface is going to operate on R's
> internal representation of R objects, called SEXPs, It won't work in other
> languages without major reworking (I can't say how major without looking
> for at the code to see if, e.g., it's just a wrapper that calls another c
> function that might be amenable to that). Even then, you'd have to look at
> if/how it deals with NAs, etc.
>
> ~G
>
> On Thu, Oct 22, 2015 at 9:59 AM, Kai Nitschke <
> kai.nitschke at uniklinik-freiburg.de> wrote:
>
>> 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
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>
>
> --
> Gabriel Becker, PhD
> Associate Scientist (Bioinformatics)
> Genentech Research
>



[Rd] C_LogLin (stats/loglin)
Kai Nitschke