给出一系列系数,
> dput(coefs)
structure(c(-0.000543450489419213, 0.293366492387962, 0.00853931616186778,
0.399449785898867, -0.000393107240288805, 0.504624470243149,
-0.0103420202744057, 0.00283255134378993), .Names = c("(Intercept)",
"x", "x2", "tx", "x:x2", "x:tx", "x2:tx", "x:x2:tx"))
如何选择所有交互,然后为所有交互添加常量?
我可以选择系数并添加一个常量(例如.1)但我如何将其与coefs
合并?
interactions=grep(':',names(coefs)) #id interactions terms
coefs.interaction=coefs[interactions]+.1
应该返回类似的内容:
(Intercept) x x2 tx x:x2 x:tx x2:tx x:x2:tx
-0.0005434505 0.2933664924 0.0085393162 0.3994497859 0.49944979 0.6046244702 0.8965798 0.1028325513
答案 0 :(得分:1)
正如我在我的问题中所描述的那样,interactions=grep(':',names(coefs))
将通过使用"选择系数来识别所有交互的位置:"在他们中。
然后,正如@Frank所评论的那样(我的解释更多),你可以这样做:
new_coefs = coefs #duplicate set of coefficients
new_coefs[interactions] <- new_coefs[interactions] + .1
请参阅new_coefs[interactions]+1
选择包含交互的变量,并允许您为它们添加常量。
然后,您可以通过设置new_coefs
等于new_coefs[interactions]
new_coefs[interactions] + .1