Is there any way to fit a `glm()` so that all levels are included (i.e. no reference level)?

前端 未结 2 548
说谎
说谎 2021-01-03 17:30

Consider the code:

x <- read.table(\"http://data.princeton.edu/wws509/datasets/cuse.dat\",
                header=TRUE)[,1:2]

fit <- glm(education ~ a         


        
相关标签:
2条回答
  • 2021-01-03 17:31

    You'll want to use the model.matrix function to convert the factors in the age variable to binary variables.

    See this answer.

    EDIT: Here is an example:

    x <- read.table("http://data.princeton.edu/wws509/datasets/cuse.dat",
                    header=TRUE)[,1:2]
    binary_variables <- model.matrix(~ x$age -1, x)
    fit <- glm(x$education ~ binary_variables, family="binomial")
    summary(fit)
    
    0 讨论(0)
  • 2021-01-03 17:52

    See ?formula, specifically, the meaning of including + 0 in your model specification...

    # Sample data - explanatory variable (continuous)
    x <- runif( 100 )
    # explanatory data, factor with 3 levels
    f <- as.factor( sample( 3 , 100 , TRUE ) )
    # outcome data
    y <- runif( 100 ) + rnorm(100) + rnorm( 100 , mean = c(1,3,6) )
    
    # model without intercept
    summary( glm( y ~ x + f + 0 ) )
    #Call:
    #glm(formula = y ~ x + f + 0)
    
    #Deviance Residuals: 
    #    Min       1Q   Median       3Q      Max  
    #-5.7316  -1.8923   0.0195   1.8918   5.9520  
    
    #Coefficients:
    #   Estimate Std. Error t value Pr(>|t|)    
    #x    0.3216     0.9772   0.329    0.743    
    #f1   3.4493     0.6823   5.055 2.06e-06 ***
    #f2   3.6349     0.6959   5.223 1.02e-06 ***
    #f3   3.1962     0.6598   4.844 4.87e-06 ***
    
    0 讨论(0)
提交回复
热议问题