0

Is there a function in R to print final form of the glm's fitted result-function? I would like to copy paste the result and use it in c# or java.

For instance this is my model:

set.seed(101)
g1=sin((1:1000)/60/pi)+runif(1000)^0.3
g2=cos((1:1000)/90/pi)+runif(1000)^0.3
gl=g1-g2+(g1*g2)+rnorm(10)/2
mymodel = lm(gl~poly(g1,3)+poly(g2,3)+poly(g1*g2,3))

I would need a polynomial function equation to use it in other application.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
jjaskulowski
  • 2,524
  • 3
  • 26
  • 36
  • I **think** what you mean is that you want to print a machine-readable string that is parseable by C#/Java in order to generate predictions from values of the predictor variables (e.g. `4.51+2.678*var1+6.783*var2`). Do you really need GLMs (i.e. complete with inverse-link function), or are linear models sufficient? Please consider including a *small* [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can better understand and more easily answer your question. – Ben Bolker Jul 16 '14 at 21:45
  • Yes, I want it in usual human readable or c#, java etc.-like form. I have included the example. – jjaskulowski Jul 16 '14 at 21:56
  • This is an improvement, but can you please give a [**reproducible**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example, including the exact output desired (hint: this one isn't)? Also, replicating orthogonal polynomials in this format will be *very* hard. Can you live with `poly(...,raw=TRUE)` ? – Ben Bolker Jul 16 '14 at 21:59
  • I can do with raw or I can do even with I(g1)+I(g1^2)+I(g1^2)+I(g2)+I(g2^2)+I(g2^2) but really there isn't anything out of the box? – jjaskulowski Jul 16 '14 at 22:11
  • 1
    Orthogonal polynomials are not simple enough to implement in one line of C#/Java code. I bet (**if** you provide a reproducible example, hint hint) someone can give a fairly simple answer to this question for cases *not* involving orthogonal polynomial basis functions computed on the fly ... More generally you might look into PMML http://cran.r-project.org/package=pmml , although probably too heavy-weight for a simple problem ... – Ben Bolker Jul 16 '14 at 22:15
  • example still doesn't quite work ("Error in poly(g1, 3) : missing values are not allowed in 'poly'") -- probably driven by negative numbers from `rnorm()` being raised to a fractional power ... – Ben Bolker Jul 17 '14 at 01:53

1 Answers1

1

This isn't completely general, but solves your specific problem and should be adaptable.

I modified the example slightly (esp. using raw=TRUE):

set.seed(101)
g1=sin((1:1000)/60/pi)+runif(1000)^0.3
g2=cos((1:1000)/90/pi)+runif(1000)^0.3
gl=g1-g2+(g1*g2)+rnorm(10)/2
mymodel <- lm(gl~poly(g1,3,raw=TRUE)+
              poly(g2,3,raw=TRUE)+poly(g1*g2,3,raw=TRUE))

cc <- coef(mymodel)
pfun <- function(x,v) {
    paste(x,paste(v,seq(length(x)),sep="^"),sep="*",
         collapse="+")
}
pfun(1:3,"x")
## [1] "1*x^1+2*x^2+3*x^3"

strwrap(paste(c(cc[1],pfun(cc[2:4],"g1"),pfun(cc[5:7],"g2"),
      pfun(cc[8:10],"(g1*g2)")),collapse="+ "))
## [1] "-0.0503143118026683+"                                                                  
## [2] "1.04495433152782*g1^1+-0.212330489393473*g1^2+0.0378835573948955*g1^3+"                
## [3] "-1.0979460962483*g2^1+-0.240393392043387*g2^2+0.0724299094173384*g2^3+"                
## [4] "1.4803777033041*(g1*g2)^1+-0.0638557942781872*(g1*g2)^2+-0.00209039494007401*(g1*g2)^3"
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453