Edin Mar
Edin Mar

Reputation: 121

How to multiply the model coefficients by a number?

Hello I would like to scale the coefficients of my model. That's why I have prepared this little example. I would like to multiply the coefficients of MPG.city, MPG.highway, Horsepower with the "correction factor". Any Ideas how to multiply or correct the coefficients?

library(MASS)
data("Cars93")

Cars_new = Cars93[,c(4,5,6,7,8,13,14)]
fit<-lm(Price~.,data=Cars_new)

#model output should be list
fit<-list(fit)

#correction factor for MPG.city, MPG.highway,Horsepower
correction<-c(1.2,1.23,1.6)

EDIT model output is a list!

model output loos like this

Upvotes: 0

Views: 292

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

You can select the coefficients by name

fit[[1]]$coefficients[c("MPG.city", "MPG.highway", "Horsepower")] <- 
   fit[[1]]$coefficients[c("MPG.city", "MPG.highway", "Horsepower")] * correction

#[[1]]

#Call:
#lm(formula = Price ~ ., data = Cars_new)

#Coefficients:
#(Intercept)    Min.Price    Max.Price     MPG.city  MPG.highway   Horsepower      RPM  
# -2.424e-02    5.010e-01    4.999e-01   -1.639e-04    1.029e-03   -1.504e-04   -1.293e-07  

Upvotes: 2

Related Questions