glm2c: C source code generator for rapid deployment of glm...

Description Usage Arguments Note Author(s) See Also Examples

Description

The glm2c() function is used to generate source code in C language implementing a given glm predictive model. It implements the following two functions; the glm_xxx_response() and glm_xxx_link(), where xxx stands for the name of the target variable of the glm object.

After the invocation of the glm2c() function two files are generated:

Usage

1
glm2c(model, filename = NULL, path = NULL)

Arguments

model

A fitted object of class "glm".

filename

OPTIONAL The name of the output file(s), the default filenames are "glm_xxx.c" and "glm_xxx.h", where xxx is the target variable's name.

path

The directory path where files are going to be saved.

Note

All numeric variables used as input to the glm object are treated as doubles, whereas factor variables are treated as strings.

Author(s)

Oscar Castro-Lopez, Ines Vega-Lopez

See Also

glm2java

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 # Example with the iris dataset with a Logical target and numeric variables,
 # using the binomial family and the logit link function
 data(iris)
 iristest = iris
 iristest$Virginica = ifelse(iristest$Species == 'virginica', TRUE,FALSE)
 iristest$Species = NULL

 # Load Package
 library(glm.deploy)
 # For repeatable results
 set.seed(123)
 # Generate the fitted glm object
 m = glm(Virginica ~ ., family = binomial(logit), data=iristest)
 # Call the glm2c() function with default filename
 glm2c(m,,tempdir())

 # Call the glm2c() function with custom filename
 glm2c(m,'my_glm_virginica', tempdir())

 # The glm2c() function generates the files: "glm_virginica.c" and
 # "glm_virginica.h"

## Not run: 
---------------Contents of the "glm_virgninica.c" file---------------

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

double glm_virginica_link(double sepal_length,
                          double sepal_width,
                          double petal_length,
                          double petal_width){
  double new_sepal_length = -2.46522019518341 * sepal_length;
  double new_sepal_width = -6.68088701405762 * sepal_width;
  double new_petal_length = 9.4293851538836 * petal_length;
  double new_petal_width = 18.2861368877881 * petal_width;

  return -42.6378038127854+new_sepal_length+
                           new_sepal_width+
                           new_petal_length+
                           new_petal_width;
}
double glm_virginica_response(double sepal_length,
                              double sepal_width,
                              double petal_length,
                              double petal_width){
  return 1/(1+exp(-glm_virginica_link(sepal_length,
                                      sepal_width,
                                      petal_length,
                                      petal_width)));
}
----End of Contents of the "glm_virgninica.c" file------------------
--------------------------------------------------------------------

-----Contents of the "glm_virgninica.h" file------------------------
double glm_virginica_link(double sepal_length,
                          double sepal_width,
                          double petal_length,
                          double petal_width);
double glm_virginica_response(double sepal_length,
                              double sepal_width,
                              double petal_length,
                              double petal_width);
-----End of Contents of the "glm_virgninica.h" file-----------------
--------------------------------------------------------------------

Usage of the functions in another programs;
1) We need to add an include line #include "virginica_glm.h" to all
source files that use library definitions.
2) Link the .c file with the library object file.
    gcc -c glm_virginica.c
3) The following is an example file "test.c" to call the functions
and print the result:

-------------------"test.c"---------------
#include <stdio.h>
#include "glm_virgnica.h" //Added to call the scoring functions.

int main(int argc, char *argv[]){
  printf("%f\n",glm_virginica_link(5.7,2.5,5.0,2.0));
  printf("%f\n",glm_virginica_response(5.7,2.5,5.0,2.0));
  return 0;
}
---------------End of "test.c"---------------
---------------------------------------------

4) Compile the "test.c" file and link it to the glm_virginica shared
library, we also need to add the "-lm" option to link it to the
math.h library:
gcc test.c -o test glm_virginica.o -lm

5) Finally Run the test.o program in linux:
./test

## End(Not run)

glm.deploy documentation built on May 1, 2019, 8:40 p.m.

Related to glm2c in glm.deploy...