Development

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Full or non constrained/reparametrized model.matrix

    5 answers - 2223 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hello!
    I have parameter estimates for a generalized linear model and would like
    to produce fitted values i.e. fitted(). This can be easily done in R,
    but my problem lies in fact that I have a vector of parameters from some
    other software, that is is not constrained i.e. I have the following
    estimates for model with one factor with 4 levels
    beta = c(intercept group1 group2 group3 group4)
    where group1:4 are estimated deviations from intercept i.e. sum to zero
    contraint, but all parameter estimates are there! How can I create a
    model matrix that will not contain any constraints since I would like to
    compute
    model.matrix("some_formula") %*% beta
    I.e. I would like to have model.matrix of the form
    1 1 0 0 0
    1 0 1 0 0
    1 0 0 1 0
    1 0 0 0 1
    and not of the following form with contr.treatment or any other contraints
    1 0 0 0
    1 1 0 0
    1 0 1 0
    1 0 0 1
    I could remove group4 from beta and use sum to zero contraint for
    contrast in fomula, but I would like to overcome this, as my model can
    be richer in number or parameters. The following example, will show what
    I would like to do:
    ## Setup
    groupN <- 4
    NPerGroup <- 5
    min <- 1
    max <- 5
    g <- runif(n = groupN, min = min, max = max)
    ## Simulate
    group <- factor(rep(paste("G", 1:groupN, sep = ""), each = NPerGroup))
    y <- vector(mode = "numeric", length = groupN * NPerGroup)
    j <- 1
    for (i in 1:groupN) {
    y[j:(i * NPerGroup)] <- rpois(n = NPerGroup, lambda = g[i])
    j <- (i * NPerGroup) + 1
    }
    ## GLM
    contrasts(group) <- contr.sum(groupN)
    fit <- glm(y ~ group, family = "poisson")
    coef(fit)
    ## Now this goes nicely
    model.matrix(y ~ group) %*% coef(fit)
    ## But pretend I have the following vector of estimated parameters
    beta <- c(coef(fit), 0 - sum(coef(fit)[-1]))
    names(beta) <- c(names(beta)[1:4], "group4")
    ## I can not apply this as model matrix does not conform to beta
    model.matrix(y ~ group) %*% beta
    ## Is there any general way of constructing full design/model matrix
    ## without any
    Thanks!
  • No.1 | | 2367 bytes | |

    5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:
    Hello!

    I have parameter estimates for a generalized linear model and would like
    to produce fitted values i.e. fitted(). This can be easily done in R,
    but my problem lies in fact that I have a vector of parameters from some
    other software, that is is not constrained i.e. I have the following
    estimates for model with one factor with 4 levels

    beta = c(intercept group1 group2 group3 group4)

    where group1:4 are estimated deviations from intercept i.e. sum to zero
    contraint, but all parameter estimates are there! How can I create a
    model matrix that will not contain any constraints since I would like to
    compute

    model.matrix("some_formula") %*% beta

    I.e. I would like to have model.matrix of the form

    1 1 0 0 0
    1 0 1 0 0
    1 0 0 1 0
    1 0 0 0 1

    and not of the following form with contr.treatment or any other contraints

    1 0 0 0
    1 1 0 0
    1 0 1 0
    1 0 0 1

    I could remove group4 from beta and use sum to zero contraint for
    contrast in fomula, but I would like to overcome this, as my model can
    be richer in number or parameters. The following example, will show what
    I would like to do:

    ## Setup

    groupN <- 4
    NPerGroup <- 5
    min <- 1
    max <- 5
    g <- runif(n = groupN, min = min, max = max)

    ## Simulate

    group <- factor(rep(paste("G", 1:groupN, sep = ""), each = NPerGroup))
    y <- vector(mode = "numeric", length = groupN * NPerGroup)
    j <- 1
    for (i in 1:groupN) {
    y[j:(i * NPerGroup)] <- rpois(n = NPerGroup, lambda = g[i])
    j <- (i * NPerGroup) + 1
    }

    ## GLM

    contrasts(group) <- contr.sum(groupN)
    fit <- glm(y ~ group, family = "poisson")
    coef(fit)

    ## Now this goes nicely
    model.matrix(y ~ group) %*% coef(fit)

    ## But pretend I have the following vector of estimated parameters
    beta <- c(coef(fit), 0 - sum(coef(fit)[-1]))
    names(beta) <- c(names(beta)[1:4], "group4")

    ## I can not apply this as model matrix does not conform to beta
    model.matrix(y ~ group) %*% beta

    Try this:

    model.matrix(y ~ group-1) %*% beta[-1] + beta[1]

    ## Is there any general way of constructing full design/model matrix
    ## without any

    Thanks!
  • No.2 | | 4572 bytes | |

    Hello!

    Thank you very much for the response. Please read within the lines

    Gabor Grothendieck wrote:
    5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:

    >Hello!
    >>

    >I have parameter estimates for a generalized linear model and would like
    >to produce fitted values i.e. fitted(). This can be easily done in R,
    >but my problem lies in fact that I have a vector of parameters from some
    >other software, that is is not constrained i.e. I have the following
    >estimates for model with one factor with 4 levels
    >>

    >beta = c(intercept group1 group2 group3 group4)
    >>

    >where group1:4 are estimated deviations from intercept i.e. sum to zero
    >contraint, but all parameter estimates are there! How can I create a
    >model matrix that will not contain any constraints since I would like to
    >compute
    >>

    >model.matrix("some_formula") %*% beta
    >>

    >I.e. I would like to have model.matrix of the form
    >>

    >1 1 0 0 0
    >1 0 1 0 0
    >1 0 0 1 0
    >1 0 0 0 1
    >>

    >and not of the following form with contr.treatment or any other
    >contraints
    >>

    >1 0 0 0
    >1 1 0 0
    >1 0 1 0
    >1 0 0 1
    >>

    >I could remove group4 from beta and use sum to zero contraint for
    >contrast in fomula, but I would like to overcome this, as my model can
    >be richer in number or parameters. The following example, will show what
    >I would like to do:
    >>

    >## Setup
    >>

    >groupN <- 4
    >NPerGroup <- 5
    >min <- 1
    >max <- 5
    >g <- runif(n = groupN, min = min, max = max)
    >>

    >## Simulate
    >>

    >group <- factor(rep(paste("G", 1:groupN, sep = ""), each = NPerGroup))
    >y <- vector(mode = "numeric", length = groupN * NPerGroup)
    >j <- 1
    >for (i in 1:groupN) {
    >y[j:(i * NPerGroup)] <- rpois(n = NPerGroup, lambda = g[i])
    >j <- (i * NPerGroup) + 1
    >}
    >>

    >## GLM
    >>

    >contrasts(group) <- contr.sum(groupN)
    >fit <- glm(y ~ group, family = "poisson")
    >coef(fit)
    >>

    >## Now this goes nicely
    >model.matrix(y ~ group) %*% coef(fit)
    >>

    >## But pretend I have the following vector of estimated parameters
    >beta <- c(coef(fit), 0 - sum(coef(fit)[-1]))
    >names(beta) <- c(names(beta)[1:4], "group4")
    >>

    >## I can not apply this as model matrix does not conform to beta
    >model.matrix(y ~ group) %*% beta


    Try this:

    model.matrix(y ~ group-1) %*% beta[-1] + beta[1]

    This is a nice hack here, but does not work in general. Say I have
    another factor

    sex <- factor(rep(paste("S", 1:2, sep = ""), times=10))

    model.matrix(y ~ group + sex - 1)

    produces a matrix of 5 columns and not 6 as I want to.

    >>

    >## Is there any general way of constructing full design/model matrix
    >## without any
    >>

    >Thanks!
    >>

    >--
    >Lep pozdrav / With regards,
    >Gregor Gorjanc
    >>

    >
    >University of Ljubljana PhD student
    >Biotechnical Faculty
    >Zootechnical Department URI:
    >Groblje 3 mail: gregor.gorjanc <atbfro.uni-lj.si
    >>

    >SI-1230 Domzale tel: +386 (0)1 72 17 861
    >Slovenia, Europe fax: +386 (0)1 72 17 888
    >>

    >
    >" must learn by doing the thing; for though you think you know it,
    >you have no certainty until you try." Sophocles ~ 450 B.C.
    >>

    >
    >R-help (AT) stat (DOT) math.ethz.ch mailing list
    >
    >PLEASE do read the posting guide!
    >
    >>
  • No.3 | | 4768 bytes | |

    5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:
    Hello!

    Thank you very much for the response. Please read within the lines

    Gabor Grothendieck wrote:
    5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:
    >
    >Hello!
    >>

    >I have parameter estimates for a generalized linear model and would like
    >to produce fitted values i.e. fitted(). This can be easily done in R,
    >but my problem lies in fact that I have a vector of parameters from some
    >other software, that is is not constrained i.e. I have the following
    >estimates for model with one factor with 4 levels
    >>

    >beta = c(intercept group1 group2 group3 group4)
    >>

    >where group1:4 are estimated deviations from intercept i.e. sum to zero
    >contraint, but all parameter estimates are there! How can I create a
    >model matrix that will not contain any constraints since I would like to
    >compute
    >>

    >model.matrix("some_formula") %*% beta
    >>

    >I.e. I would like to have model.matrix of the form
    >>

    >1 1 0 0 0
    >1 0 1 0 0
    >1 0 0 1 0
    >1 0 0 0 1
    >>

    >and not of the following form with contr.treatment or any other
    >contraints
    >>

    >1 0 0 0
    >1 1 0 0
    >1 0 1 0
    >1 0 0 1
    >>

    >I could remove group4 from beta and use sum to zero contraint for
    >contrast in fomula, but I would like to overcome this, as my model can
    >be richer in number or parameters. The following example, will show what
    >I would like to do:
    >>

    >## Setup
    >>

    >groupN <- 4
    >NPerGroup <- 5
    >min <- 1
    >max <- 5
    >g <- runif(n = groupN, min = min, max = max)
    >>

    >## Simulate
    >>

    >group <- factor(rep(paste("G", 1:groupN, sep = ""), each = NPerGroup))
    >y <- vector(mode = "numeric", length = groupN * NPerGroup)
    >j <- 1
    >for (i in 1:groupN) {
    >y[j:(i * NPerGroup)] <- rpois(n = NPerGroup, lambda = g[i])
    >j <- (i * NPerGroup) + 1
    >}
    >>

    >## GLM
    >>

    >contrasts(group) <- contr.sum(groupN)
    >fit <- glm(y ~ group, family = "poisson")
    >coef(fit)
    >>

    >## Now this goes nicely
    >model.matrix(y ~ group) %*% coef(fit)
    >>

    >## But pretend I have the following vector of estimated parameters
    >beta <- c(coef(fit), 0 - sum(coef(fit)[-1]))
    >names(beta) <- c(names(beta)[1:4], "group4")
    >>

    >## I can not apply this as model matrix does not conform to beta
    >model.matrix(y ~ group) %*% beta
    >
    >

    Try this:

    model.matrix(y ~ group-1) %*% beta[-1] + beta[1]

    This is a nice hack here, but does not work in general. Say I have
    another factor

    sex <- factor(rep(paste("S", 1:2, sep = ""), times=10))

    model.matrix(y ~ group + sex - 1)

    produces a matrix of 5 columns and not 6 as I want to.

    Try this:

    cbind(1, model.matrix(~group-1), model.matrix(~sex-1))


    >
    >>

    >## Is there any general way of constructing full design/model matrix
    >## without any
    >>

    >Thanks!
    >>

    >--
    >Lep pozdrav / With regards,
    >Gregor Gorjanc
    >>

    >
    >University of Ljubljana PhD student
    >Biotechnical Faculty
    >Zootechnical Department URI:
    >Groblje 3 mail: gregor.gorjanc <atbfro.uni-lj.si
    >>

    >SI-1230 Domzale tel: +386 (0)1 72 17 861
    >Slovenia, Europe fax: +386 (0)1 72 17 888
    >>

    >
    >" must learn by doing the thing; for though you think you know it,
    >you have no certainty until you try." Sophocles ~ 450 B.C.
    >>

    >
    >R-help (AT) stat (DOT) math.ethz.ch mailing list
    >
    >PLEASE do read the posting guide!
    >
    >>

    >
    >
  • No.4 | | 4054 bytes | |

    5/9/06, Gabor Grothendieck <ggrothendieck (AT) gmail (DOT) comwrote:
    5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:
    Hello!

    Thank you very much for the response. Please read within the lines

    Gabor Grothendieck wrote:
    5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:
    >
    >Hello!
    >>

    >I have parameter estimates for a generalized linear model and would like
    >to produce fitted values i.e. fitted(). This can be easily done in R,
    >but my problem lies in fact that I have a vector of parameters from some
    >other software, that is is not constrained i.e. I have the following
    >estimates for model with one factor with 4 levels
    >>

    >beta = c(intercept group1 group2 group3 group4)
    >>

    >where group1:4 are estimated deviations from intercept i.e. sum to zero
    >contraint, but all parameter estimates are there! How can I create a
    >model matrix that will not contain any constraints since I would like to
    >compute
    >>

    >model.matrix("some_formula") %*% beta
    >>

    >I.e. I would like to have model.matrix of the form
    >>

    >1 1 0 0 0
    >1 0 1 0 0
    >1 0 0 1 0
    >1 0 0 0 1
    >>

    >and not of the following form with contr.treatment or any other
    >contraints
    >>

    >1 0 0 0
    >1 1 0 0
    >1 0 1 0
    >1 0 0 1
    >>

    >I could remove group4 from beta and use sum to zero contraint for
    >contrast in fomula, but I would like to overcome this, as my model can
    >be richer in number or parameters. The following example, will show what
    >I would like to do:
    >>

    >## Setup
    >>

    >groupN <- 4
    >NPerGroup <- 5
    >min <- 1
    >max <- 5
    >g <- runif(n = groupN, min = min, max = max)
    >>

    >## Simulate
    >>

    >group <- factor(rep(paste("G", 1:groupN, sep = ""), each = NPerGroup))
    >y <- vector(mode = "numeric", length = groupN * NPerGroup)
    >j <- 1
    >for (i in 1:groupN) {
    >y[j:(i * NPerGroup)] <- rpois(n = NPerGroup, lambda = g[i])
    >j <- (i * NPerGroup) + 1
    >}
    >>

    >## GLM
    >>

    >contrasts(group) <- contr.sum(groupN)
    >fit <- glm(y ~ group, family = "poisson")
    >coef(fit)
    >>

    >## Now this goes nicely
    >model.matrix(y ~ group) %*% coef(fit)
    >>

    >## But pretend I have the following vector of estimated parameters
    >beta <- c(coef(fit), 0 - sum(coef(fit)[-1]))
    >names(beta) <- c(names(beta)[1:4], "group4")
    >>

    >## I can not apply this as model matrix does not conform to beta
    >model.matrix(y ~ group) %*% beta
    >
    >

    Try this:

    model.matrix(y ~ group-1) %*% beta[-1] + beta[1]

    This is a nice hack here, but does not work in general. Say I have
    another factor

    sex <- factor(rep(paste("S", 1:2, sep = ""), times=10))

    model.matrix(y ~ group + sex - 1)

    produces a matrix of 5 columns and not 6 as I want to.

    Try this:

    cbind(1, model.matrix(~group-1), model.matrix(~sex-1))

    In thinking about this a bit more, try this:

    attr(group, "contrasts") <- diag(nlevels(group))
    attr(sex, "contrasts") <- diag(nlevels(sex))
    model.matrix(~ group + sex)

    R-help (AT) stat (DOT) math.ethz.ch mailing list

    PLEASE do read the posting guide!
  • No.5 | | 5069 bytes | |

    Gabor thanks! Last suggestion works like charm!

    Gabor Grothendieck wrote:
    5/9/06, Gabor Grothendieck <ggrothendieck (AT) gmail (DOT) comwrote:

    >5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:
    >Hello!
    >>

    >Thank you very much for the response. Please read within the lines
    >>

    >Gabor Grothendieck wrote:
    >5/9/06, Gregor Gorjanc <gregor.gorjanc (AT) gmail (DOT) comwrote:
    >>
    >>Hello!
    >>>

    >>I have parameter estimates for a generalized linear model and

    >would like
    >>to produce fitted values i.e. fitted(). This can be easily done

    >in R,
    >>but my problem lies in fact that I have a vector of parameters

    >from some
    >>other software, that is is not constrained i.e. I have the following
    >>estimates for model with one factor with 4 levels
    >>>

    >>beta = c(intercept group1 group2 group3 group4)
    >>>

    >>where group1:4 are estimated deviations from intercept i.e. sum

    >to zero
    >>contraint, but all parameter estimates are there! How can I create a
    >>model matrix that will not contain any constraints since I would

    >like to
    >>compute
    >>>

    >>model.matrix("some_formula") %*% beta
    >>>

    >>I.e. I would like to have model.matrix of the form
    >>>

    >>1 1 0 0 0
    >>1 0 1 0 0
    >>1 0 0 1 0
    >>1 0 0 0 1
    >>>

    >>and not of the following form with contr.treatment or any other
    >>contraints
    >>>

    >>1 0 0 0
    >>1 1 0 0
    >>1 0 1 0
    >>1 0 0 1
    >>>

    >>I could remove group4 from beta and use sum to zero contraint for
    >>contrast in fomula, but I would like to overcome this, as my

    >model can
    >>be richer in number or parameters. The following example, will

    >show what
    >>I would like to do:
    >>>

    >>## Setup
    >>>

    >>groupN <- 4
    >>NPerGroup <- 5
    >>min <- 1
    >>max <- 5
    >>g <- runif(n = groupN, min = min, max = max)
    >>>

    >>## Simulate
    >>>

    >>group <- factor(rep(paste("G", 1:groupN, sep = ""), each =

    >NPerGroup))
    >>y <- vector(mode = "numeric", length = groupN * NPerGroup)
    >>j <- 1
    >>for (i in 1:groupN) {
    >>y[j:(i * NPerGroup)] <- rpois(n = NPerGroup, lambda = g[i])
    >>j <- (i * NPerGroup) + 1
    >>}
    >>>

    >>## GLM
    >>>

    >>contrasts(group) <- contr.sum(groupN)
    >>fit <- glm(y ~ group, family = "poisson")
    >>coef(fit)
    >>>

    >>## Now this goes nicely
    >>model.matrix(y ~ group) %*% coef(fit)
    >>>

    >>## But pretend I have the following vector of estimated parameters
    >>beta <- c(coef(fit), 0 - sum(coef(fit)[-1]))
    >>names(beta) <- c(names(beta)[1:4], "group4")
    >>>

    >>## I can not apply this as model matrix does not conform to beta
    >>model.matrix(y ~ group) %*% beta
    >>
    >>

    >Try this:
    >>

    >model.matrix(y ~ group-1) %*% beta[-1] + beta[1]
    >>

    >This is a nice hack here, but does not work in general. Say I have
    >another factor
    >>

    >sex <- factor(rep(paste("S", 1:2, sep = ""), times=10))
    >>

    >model.matrix(y ~ group + sex - 1)
    >>

    >produces a matrix of 5 columns and not 6 as I want to.
    >>

    >Try this:
    >>

    >cbind(1, model.matrix(~group-1), model.matrix(~sex-1))
    >>


    In thinking about this a bit more, try this:

    attr(group, "contrasts") <- diag(nlevels(group))
    attr(sex, "contrasts") <- diag(nlevels(sex))
    model.matrix(~ group + sex)

Re: Full or non constrained/reparametrized model.matrix


max 4000 letters.
Your nickname that display:
In order to stop the spam: 8 + 7 =
QUESTION ON "Development"

EMSDN.COM