Christoph Häni
2013-Oct-07 13:26 UTC
[R] GLM: Defining non-constant variance for (gaussian) family
Dear all,
I want to fit some observations Y to a set of predictor variables X_i. (and
proceed with model selection with support of the second-order AIC
(AICc)...)
I (think I) "know" that the distribution of Y[i] is Gaussian and has a
variance, which is proportional to its value Y[i]. Say:
x1 <- rnorm(1:100)*2
x2 <- runif(1:100)*0.5
etc...
Y <- exp(3.2 + 0.45*x1 - 0.5*x2) + rnorm(100,0,(exp(3.2 + 0.45*x1 -
0.5*x2))^0.5)
So, I thought, I could change the family object as following:
GaussVar <- gaussian("log")
GaussVar$variance <- function(mu) mu
GaussVar$dev.resids <- function(y, mu, wt) wt * ((y - mu)^2/mu)
GaussVar$aic <- function (y, n, mu, wt, dev){
nobs <- length(y);-2 * sum(dnorm(y,mu,sqrt(dev*mu/nobs), log = TRUE) * wt)
+ 2
}
And the resulting Regression:
reg <- glm(Y~x1+x2,family=GaussVar)
Is this a valid modification of the "family" object?
Am I missing something?
Is the calculation of the AIC complete rubbish?
I have to admit, I'm very new to the topic of regression, so please be
kind! :-)
Thanks in advance,
Christoph
[[alternative HTML version deleted]]