dummy.coef() 用于多元模型
我想知道 dummy.coef() 中的一些内容,它将方差分析模型中的估计参数(对比度)转换为原始参数。它仅适用于单变量模型,但使其适用于多变量模型所需的更改似乎很小。在 dummy.coef.lm() 中:
- 第 52 行 coef <- object$coefficients 必须是 coef <- as.matrix(object$coefficients ) 以适应单变量和多变量模型(
coef(object)
在第一种情况下是一个向量,在第二种情况下是一个矩阵) - 第 60 行ans <- drop(mm[rn == tl[j], keep, drop = FALSE] %*% coef[keep]) 必须是 ans <- drop( mm[rn == tl[j], keep, drop = FALSE] %*% coef[keep, ]) 保留 coef
- 第 61 行 中的所有列
names(ans) <- rnn[rn == tl[j]]
可以是names(ans) <-rep(rnn[rn == tl[j]], ncol (coef))
为所有列的行命名
打印方法需要一些更改,但似乎就是这样。 有人知道为什么 dummy.coef()
不是为处理多元模型而设计的吗?
我偶然发现的另一件事:第 20-22 行
for (i in vars) args[[i]] <- if (nxl[[i]] == 1)
rep.int(1, nl)
else factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]])
安全吗?即,如果 if()
子句为 TRUE
,是否会出现意外的 else
?我本来期望类似的东西
for (i in vars) args[[i]] <- if (nxl[[i]] == 1) {
rep.int(1, nl)
} else { factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]]) }
I was wondering about some things in dummy.coef()
which converts the estimated parameters (contrasts) in ANOVA models to the original ones. It only works for univariate models, but the changes required to also make it work for multivariate models seem minor. In dummy.coef.lm()
:
- line 52
coef <- object$coefficients
would have to becoef <- as.matrix(object$coefficients)
to accomodate univariate and multivariate models (coef(object)
is a vector in the 1st case, and a matrix in the 2nd) - line 60
ans <- drop(mm[rn == tl[j], keep, drop = FALSE] %*% coef[keep])
would have to beans <- drop(mm[rn == tl[j], keep, drop = FALSE] %*% coef[keep, ])
to keep all columns incoef
- line 61
names(ans) <- rnn[rn == tl[j]]
could benames(ans) <- rep(rnn[rn == tl[j]], ncol(coef))
to give names to the rows of all columns
The printing method would need some changes, but that seems to be it. Does anybody know why dummy.coef()
was not designed to handle multivariate models?
Another thing I stumbled upon: Lines 20-22 are
for (i in vars) args[[i]] <- if (nxl[[i]] == 1)
rep.int(1, nl)
else factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]])
Is that safe? I.e., if the if()
clause is TRUE
, wouldn't there be an unexpected else
? I would have expected something like
for (i in vars) args[[i]] <- if (nxl[[i]] == 1) {
rep.int(1, nl)
} else { factor(rep.int(xl[[i]][1L], nl), levels = xl[[i]]) }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这仅解决了您的第二个问题(关于代码中第 20-22 行的
if() x else y
语句的问题)。首先,尝试将这两个块剪切并粘贴到 R 会话中:
发生了什么?
{}
在这里发挥了重要作用。块 1 逐行“读取-解析-评估”代码,导致出现您所期望的问题。另一方面,在进行任何评估之前会读取并完全解析块 2。这是{}
指示 R 执行的操作的一部分。当解析器接收到整个代码块时,它会明确地将if() x else y
块解析为一个表达式。您引用的代码取自函数体内(即
{}
对内部)。在这种情况下,它被正确处理(即像块 2 一样)。华泰
This only addresses your second question (the one about the
if() x else y
statement at lines 20-22 in the code).To start with, try cut-and-pasting these two blocks into an R session:
What's going on? The
{}
make all the difference here. Block 1 'read-parse-evaluates' the code line-by-line, causing just the problem you'd expect. Block 2, on the other hand, is read and completely parsed before any evaluation takes place. That's part of what{}
directs R to do. When it receives the block of code as a whole, the parser clearly parses theif() x else y
block as one expression.The code you quoted was taken from inside the body of a function (i.e. inside of a
{}
pair). In that context, it is handled correctly (i.e. like Block 2).HTH
我们有一份关于
dummy.coef
的官方错误报告 带有我现在正在查看的“某种”补丁 - 看起来不错,尽管一次更改的内容比我预期的要多一些。当我在谷歌上搜索
dummy.coef
的其他问题时,我得到了这个旧的 SO 线程。让我回答(作为 R 核心团队的成员)我们如果代码(和文档)变化不大,则对多元模型的扩展感兴趣。如果您有兴趣提供帮助,请回复。
We have an official bug report about
dummy.coef
with a "kind of" patch at which I am looking now -- it seems good, even though changing a bit more things at once than I was expecting.When I was googling for other problems with
dummy.coef
I got this old SO thread.Let me answer (as a member of the R core team) that we are interested in the extension to multivariate models if the code (and documentation) changes are not large. Please reply if you are interested in helping.