R 中 SAS 参数估计的等价物是什么
请问这怎么用R
proc glm data=DataTX;
class DAG;
by HID;
model Bwt = DAG/ss3 solution;
ods output parameterestimates =TX_BW_corrFact;
run来写;
Please how can this be written in R
proc glm data=DataTX;
class DAG;
by HID;
model Bwt = DAG/ss3 solution;
ods output parameterestimates =TX_BW_corrFact;
run;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 R 中的大多数用途中,与 proc glm 等效的是 lm,它适合线性模型。看起来您想要模型的估计系数,可以通过 coef(mod) 获得,其中 mod 是 lm 返回的对象/代码>。
最复杂的部分是复制
by
语句,它适合by
变量的每个级别(在本例中为HID
)的单独模型。尝试这样的事情。我假设您已经将数据集导入到 R 中。这将根据
HID
将DataTX
分成不同的组。对于每个组,它都会拟合模型lm(Bwt ~ DAG)
。最后一行提取每个模型的拟合系数。这可以连接成一行,但将其保留为 3 个单独的语句可能会更容易理解。
请注意,系数与 SAS 的系数不同,因为两个系统参数化模型的方式存在差异。特别是,SAS 默认将类/因子变量的最后一个级别视为参考,而 R 使用第一个。
The equivalent to
proc glm
for most purposes in R islm
, which fits linear models. It looks like you want the estimated coefficients from the model(s), which can be obtained bycoef(mod)
wheremod
is the object returned bylm
.The most complicated bit is replicating the
by
statement, which fits separate models for each level of theby
variable (HID
in this case). Try something like this. I assume you've already got your dataset imported into R.This splits
DataTX
into separate groups based onHID
. For each group, it then fits the modellm(Bwt ~ DAG)
. The last line then extracts the fitted coefficients for each model.This can be concatenated into a single line, but leaving it as 3 separate statements probably makes it easier to follow.
Note that the coefficients won't be the same as those from SAS, because of differences in how the two systems parametrise the model. In particular, SAS by default treats the last level of a class/factor variable as the reference, while R uses the first.
看看 lme4 或 nlme 包中的 lmList()
它比 Hong 的解决方案短。
Have a look at lmList() from the lme4 or nlme package
That is shorter than Hong's solution.