simglm: Simulate Data from Generalized Linear Models

View source: R/rsq.R

simglmR Documentation

Simulate Data from Generalized Linear Models

Description

Simulate data from linear and generalized linear models. Only the first covariate truely affects the response variable with coefficient equal to lambda.

Usage

simglm(family=c("binomial", "gaussian", "poisson","Gamma"),lambda=3,n=50,p=3)

Arguments

family

the family of the distribution.

lambda

size of the coefficient of the first covariate.

n

the sample size.

p

the number of covarites.

Details

The first covariate takes 1 in half of the observations, and 0 or -1 in the other half. When lambda gets larger, it is supposed to easier to predict the response variable.

Value

Returned values include yx and beta.

yx

a data frame including the response y and covariates x.1, x.2, and so on.

beta

true values of the regression coefficients.

Author(s)

Dabao Zhang, Department of Statistics, Purdue University

References

Zhang, D. (2017). A coefficient of determination for generalized linear models. The American Statistician, 71(4): 310-316.

See Also

rsq, rsq.partial, pcor.

Examples

# Poisson Models
sdata <- simglm(family="poisson",lambda=4)
fitf <- glm(y~x.1+x.2+x.3,family=poisson,data=sdata$yx)
rsq(fitf)  # type='v'

fitr <- glm(y~x.2+x.3,family=poisson,data=sdata$yx)
rsq(fitr)  # type='v'
rsq(fitr,type='kl')
rsq(fitr,type='lr')
rsq(fitr,type='n')

pcor(fitr)  # type='v'
pcor(fitr,type='kl')
pcor(fitr,type='lr')
pcor(fitr,type='n')

# Gamma models with shape=100
n <- 50
sdata <- simglm(family="Gamma",lambda=4,n=n)
fitf <- glm(y~x.1+x.2+x.3,family=Gamma,data=sdata$yx)
rsq(fitf)  # type='v'
rsq.partial(fitf)  # type='v'

fitr <- glm(y~x.2,family=Gamma,data=sdata$yx)
rsq(fitr)  # type='v'
rsq(fitr,type='kl')
rsq(fitr,type='lr')
rsq(fitr,type='n')

# Likelihood-ratio-based R-squared
y <- sdata$yx$y
yhatr <- fitr$fitted.values
fit0 <- update(fitr,.~1)
yhat0 <- fit0$fitted.values
llr <- sum(log(dgamma(y,shape=100,scale=yhatr/100)))
ll0 <- sum(log(dgamma(y,shape=100,scale=yhat0/100)))

# Likelihood-ratio-based R-squared
1-exp(-2*(llr-ll0)/n)

# Corrected likelihood-ratio-based R-squared
(1-exp(-2*(llr-ll0)/n))/(1-exp(2*ll0/n))

rsq documentation built on Oct. 22, 2023, 5:07 p.m.

Related to simglm in rsq...