[Rd] predict.glm line 28. Please explain

2014-01-13 Thread Paul Johnson
I imitated predict.glm, my thing worked, now I need to revise.  It would
help me very much if someone would explain predict.glm line 28, which says

object$na.action <- NULL # kill this for predict.lm calls

I want to know

1) why does it set the object$na.action to NULL
2) what does the comment after mean?

Maybe I need a pass by value lesson too, because I can't see how changing
that in object would have any effect on calculations done elsewhere.

pj


Here's the context from predict.glm, to save you trouble of looking it up:

predict.glm <-
  function(object, newdata = NULL, type = c("link", "response", "terms"),
   se.fit = FALSE, dispersion = NULL, terms = NULL,
   na.action = na.pass, ...)
{
## 1998/06/23 KH:  predict.lm() now merged with the version in lm.R

type <- match.arg(type)
na.act <- object$na.action
object$na.action <- NULL # kill this for predict.lm calls
if (!se.fit) {
## No standard errors
if(missing(newdata)) {
pred <- switch(type,
   link = object$linear.predictors,
   response = object$fitted.values,
   terms = predict.lm(object,  se.fit = se.fit,
   scale = 1, type = "terms", terms = terms)
   )
if(!is.null(na.act)) pred <- napredict(na.act, pred)
} else {
pred <- predict.lm(object, newdata, se.fit, scale = 1,
   type = ifelse(type == "link", "response",
type),
   terms = terms, na.action = na.action)
switch(type,
   response = {pred <- family(object)$linkinv(pred)},
   link = , terms = )
  }
} else {
## summary.survreg has no ... argument.
if(inherits(object, "survreg")) dispersion <- 1.
if(is.null(dispersion) || dispersion == 0)
dispersion <- summary(object, dispersion=dispersion)$dispersion
residual.scale <- as.vector(sqrt(dispersion))
pred <- predict.lm(object, newdata, se.fit, scale = residual.scale,
   type = ifelse(type == "link", "response", type),
   terms = terms, na.action = na.action)
fit <- pred$fit
se.fit <- pred$se.fit
switch(type,
   response = {
   se.fit <- se.fit * abs(family(object)$mu.eta(fit))
   fit <- family(object)$linkinv(fit)
   },
   link = , terms = )
if( missing(newdata) && !is.null(na.act) ) {
fit <- napredict(na.act, fit)
se.fit <- napredict(na.act, se.fit)
}
pred <- list(fit = fit, se.fit = se.fit, residual.scale =
residual.scale)
}
pred
}



-- 
Paul E. Johnson
Professor, Political Science  Assoc. Director
1541 Lilac Lane, Room 504  Center for Research Methods
University of Kansas University of Kansas
http://pj.freefaculty.org   http://quant.ku.edu

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] predict.glm line 28. Please explain

2014-01-13 Thread Gabriel Becker
Paul,

These are inferences from the code you included. I didn't write and have
not looked particularly closely at predict.glm...

object is getting passed to predict.lm in certain situations by the code
you included. This is how altering the object will affect behavior. The
inner function calls get passed the modified version, not the one
originally passed into the outer function.

 Specifically you can see that if the new data is missing and the
prediction type is "terms", predict.lm is called on object without
na.action being set.

I would conjecture this may be because the other cases of that switch
statement don't call predict.lm, but they all need to have the NA action
happen on them, which is why napredict is called directly after that switch
statement.

There could be other reasons, though, or I could simply be wrong altogether.

~G


On Mon, Jan 13, 2014 at 2:21 PM, Paul Johnson  wrote:

> I imitated predict.glm, my thing worked, now I need to revise.  It would
> help me very much if someone would explain predict.glm line 28, which says
>
> object$na.action <- NULL # kill this for predict.lm calls
>
> I want to know
>
> 1) why does it set the object$na.action to NULL
> 2) what does the comment after mean?
>
> Maybe I need a pass by value lesson too, because I can't see how changing
> that in object would have any effect on calculations done elsewhere.
>
> pj
>
>
> Here's the context from predict.glm, to save you trouble of looking it up:
>
> predict.glm <-
>   function(object, newdata = NULL, type = c("link", "response", "terms"),
>se.fit = FALSE, dispersion = NULL, terms = NULL,
>na.action = na.pass, ...)
> {
> ## 1998/06/23 KH:  predict.lm() now merged with the version in lm.R
>
> type <- match.arg(type)
> na.act <- object$na.action
> object$na.action <- NULL # kill this for predict.lm calls
> if (!se.fit) {
> ## No standard errors
> if(missing(newdata)) {
> pred <- switch(type,
>link = object$linear.predictors,
>response = object$fitted.values,
>terms = predict.lm(object,  se.fit = se.fit,
>scale = 1, type = "terms", terms = terms)
>)
> if(!is.null(na.act)) pred <- napredict(na.act, pred)
> } else {
> pred <- predict.lm(object, newdata, se.fit, scale = 1,
>type = ifelse(type == "link", "response",
> type),
>terms = terms, na.action = na.action)
> switch(type,
>response = {pred <- family(object)$linkinv(pred)},
>link = , terms = )
>   }
> } else {
> ## summary.survreg has no ... argument.
> if(inherits(object, "survreg")) dispersion <- 1.
> if(is.null(dispersion) || dispersion == 0)
> dispersion <- summary(object, dispersion=dispersion)$dispersion
> residual.scale <- as.vector(sqrt(dispersion))
> pred <- predict.lm(object, newdata, se.fit, scale = residual.scale,
>type = ifelse(type == "link", "response", type),
>terms = terms, na.action = na.action)
> fit <- pred$fit
> se.fit <- pred$se.fit
> switch(type,
>response = {
>se.fit <- se.fit * abs(family(object)$mu.eta(fit))
>fit <- family(object)$linkinv(fit)
>},
>link = , terms = )
> if( missing(newdata) && !is.null(na.act) ) {
> fit <- napredict(na.act, fit)
> se.fit <- napredict(na.act, se.fit)
> }
> pred <- list(fit = fit, se.fit = se.fit, residual.scale =
> residual.scale)
> }
> pred
> }
>
>
>
> --
> Paul E. Johnson
> Professor, Political Science  Assoc. Director
> 1541 Lilac Lane, Room 504  Center for Research Methods
> University of Kansas University of Kansas
> http://pj.freefaculty.org   http://quant.ku.edu
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Gabriel Becker
Graduate Student
Statistics Department
University of California, Davis

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Passing a large array to .Call by reference

2014-01-13 Thread Boris Aronshtam
I need to pass a large array to my C function. I am using .Call(). I can 
clearly see according to the memory consumption (reported by gc()) that the 
array is duplicated. Is there a way to avoid this duplication and pass the 
array by reference?



[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Passing a large array to .Call by reference

2014-01-13 Thread Dirk Eddelbuettel

On 14 January 2014 at 02:02, Boris Aronshtam wrote:
| I need to pass a large array to my C function. I am using .Call(). I can 
clearly see according to the memory consumption (reported by gc()) that the 
array is duplicated. Is there a way to avoid this duplication and pass the 
array by reference?

Staring at gc() is at approximate at best. 

R has memory profiling, see Writing R Extensions for how to use it.

There is no code in your post, so not much else I can comment on.

Dirk

-- 
Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel