Search code examples
rregressionlinear-regressiontransformation

How to set a coefficient at a particular value, and retain the predictor in the model summary?


I am running a linear regression of the type below:

y <- lm(x ~ z, data)

I want z set to 0.8, and then I want to be able to extract the resulting estimate for z from the model output using the tidy function. I have had a look at offset(), but I am unable to see the z estimate in the model output, which I need for a summary table. Does it suffice to simply include I(z*0.8)? This would result in the below code:

y <- lm(x ~ I(z*0.8), data)

Solution

  • I would recommend ggeffects. For example:

    
    library(ggeffects)
    #> Warning: package 'ggeffects' was built under R version 3.6.2
    library(ggplot2)
    #> Registered S3 methods overwritten by 'ggplot2':
    #>   method         from 
    #>   [.quosures     rlang
    #>   c.quosures     rlang
    #>   print.quosures rlang
    data(efc)
    fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)
    
    mydf <- ggpredict(fit, terms = c("c12hour [30:80]", "c172code [1,3]"))
    mydf
    #> # Predicted values of Total score BARTHEL INDEX
    #> # x = average number of hours of care per week
    #> 
    #> # c172code = low level of education
    #> 
    #>  x | Predicted |         95% CI
    #> -------------------------------
    #> 30 |     67.15 | [64.04, 70.26]
    #> 38 |     65.12 | [62.06, 68.18]
    #> 47 |     62.84 | [59.81, 65.88]
    #> 55 |     60.81 | [57.78, 63.85]
    #> 63 |     58.79 | [55.72, 61.85]
    #> 80 |     54.48 | [51.28, 57.68]
    #> 
    #> # c172code = high level of education
    #> 
    #>  x | Predicted |         95% CI
    #> -------------------------------
    #> 30 |     68.58 | [65.42, 71.75]
    #> 38 |     66.56 | [63.39, 69.73]
    #> 47 |     64.28 | [61.08, 67.47]
    #> 55 |     62.25 | [59.01, 65.50]
    #> 63 |     60.23 | [56.91, 63.54]
    #> 80 |     55.92 | [52.39, 59.45]
    #> 
    #> Adjusted for:
    #> * neg_c_7 = 11.84
    #> * c161sex =  1.76
    
    ggplot(mydf, aes(x, predicted, colour = group)) + geom_line()
    

    Created on 2020-12-04 by the reprex package (v0.3.0)

    From here