Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

r - Set one or more of coefficients to a specific integer

I am using a standard lm model and would like to set the coefficients of one or more of my variables to a specific integer.

For example, I would like the coefficient of my weather and price variables to be 647 and 15 respectively.

I am using the lm function with a standard formula.

The closest things I've found so far are the offset function within glm, or restrict.rhs within systemfit.

I've also looked at subtracting the total contribution from these variables with their coefficients set, but this is not very scalable.

I'm aware of all the issues setting a coefficient has, but would like to do so just from an exploratory point of view.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the offset term in the formula and include the desired coefficient and variable therein:

df<-data.frame(aa=1:6,bb=2:7,cc=c(4,2,7,5,8,3))

lm(cc ~ aa + offset(647*bb), data = df)

So this is regressing cc on aa plus the fixed term bb * 647. For more than one given coefficient, add the appropriate additional offset() terms.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...