[R] Saving fits (glm, nls) without data
David Winsemius
dwinsemius at comcast.net
Tue Sep 7 21:37:13 CEST 2010
On Sep 7, 2010, at 3:16 PM, David Winsemius wrote:
>
> On Sep 7, 2010, at 2:53 PM, Johann Hibschman wrote:
>
>> David Winsemius <dwinsemius at comcast.net> writes:
>>
>>> On Sep 7, 2010, at 11:02 AM, Johann Hibschman wrote:
>>>> Even so, I would prefer to only save the coefficients
>>>
>>> Have you read through the Value section of glm's help page?
>>>
>>> ...and
>>>
>>> ?coef
>>
>> I have; it's easy to get the coefficients. The part I'm struggling
>> with
>> is reconstituting an operational glm object given the coefficient
>> vector
>> and the formula.
>>
>> Really, I was hoping that someone had already done this work, so I
>> could
>> stop trying re-create the right kind of term object, while making
>> sure
>> it doesn't hold on to a pointer to an environment with a lot of
>> data in
>> it, etc., etc..
>
> I was assuming you could take all the code work that was already
> tested and trim out the non essential code and arguments let it work
> on a new class.
>
Just tested my theory and it seems to be holding up. Took the example
on the predict help page, set three of the variable length components
not needed in the predict operations to NULL and the code still runs
fine. It does not appear that either predict.glm or predict.lm check
to see if there are any missing components:
> ldose <- rep(0:5, 2)
> numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
> sex <- factor(rep(c("M", "F"), c(6, 6)))
> SF <- cbind(numdead, numalive=20-numdead)
> budworm.lg <- glm(SF ~ sex*ldose, family=binomial)
> budworm.lg["residuals"] <- NULL
> budworm.lg["linear.predictors"] <- NULL
> budworm.lg["fitted.values"] <- NULL
> plot(c(1,32), c(0,1), type = "n", xlab = "dose",
+ ylab = "prob", log = "x")
> text(2^ldose, numdead/20, as.character(sex))
> ld <- seq(0, 5, 0.1)
> lines(2^ld, predict(budworm.lg, data.frame(ldose=ld,
+ sex=factor(rep("M", length(ld)), levels=levels(sex))),
+ type = "response"))
Also took out "y", "qr", "weights", and "prior.weights"
> budworm.lg["y"] <- NULL
> budworm.lg["weights"] <- NULL
> budworm.lg["prior.weights"] <- NULL
> budworm.lg["qr"] <- NULL
... And it continue to perform without throwing an error.
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list