R 中 SAS 参数估计的等价物是什么

发布于 2024-11-28 01:50:31 字数 174 浏览 1 评论 0原文

请问这怎么用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-12-05 01:50:31

在 R 中的大多数用途中,与 proc glm 等效的是 lm,它适合线性模型。看起来您想要模型的估计系数,可以通过 coef(mod) 获得,其中 mod 是 lm 返回的对象/代码>。

最复杂的部分是复制 by 语句,它适合 by 变量的每个级别(在本例中为 HID)的单独模型。尝试这样的事情。我假设您已经将数据集导入到 R 中。

grps <- split(DataTX, DataTX$HID)
mods <- lapply(grps, function(x) lm(Bwt ~ DAG, data=x))
sapply(mods, coef)

这将根据 HIDDataTX 分成不同的组。对于每个组,它都会拟合模型 lm(Bwt ~ DAG)。最后一行提取每个模型的拟合系数。

这可以连接成一行,但将其保留为 3 个单独的语句可能会更容易理解。

请注意,系数与 SAS 的系数不同,因为两个系统参数化模型的方式存在差异。特别是,SAS 默认将类/因子变量的最后一个级别视为参考,而 R 使用第一个。

The equivalent to proc glm for most purposes in R is lm, which fits linear models. It looks like you want the estimated coefficients from the model(s), which can be obtained by coef(mod) where mod is the object returned by lm.

The most complicated bit is replicating the by statement, which fits separate models for each level of the by variable (HID in this case). Try something like this. I assume you've already got your dataset imported into R.

grps <- split(DataTX, DataTX$HID)
mods <- lapply(grps, function(x) lm(Bwt ~ DAG, data=x))
sapply(mods, coef)

This splits DataTX into separate groups based on HID. For each group, it then fits the model lm(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.

你丑哭了我 2024-12-05 01:50:31

看看 lme4 或 nlme 包中的 lmList()

library(lme4)
lmList(Reaction ~ Days | Subject, sleepstudy)

它比 Hong 的解决方案短。

grps <- split(sleepstudy, sleepstudy$Subject)
mods <- lapply(grps, function(x) lm(Reaction ~ Days, data=x))
sapply(mods, coef)

Have a look at lmList() from the lme4 or nlme package

library(lme4)
lmList(Reaction ~ Days | Subject, sleepstudy)

That is shorter than Hong's solution.

grps <- split(sleepstudy, sleepstudy$Subject)
mods <- lapply(grps, function(x) lm(Reaction ~ Days, data=x))
sapply(mods, coef)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文