作者andrew43 (讨厌有好心推文後删文者)
看板R_Language
标题Re: [问题] lack of fit for multiple regression
时间Wed Apr 30 18:00:21 2014
y <- c(8,13,12,11,9,8,7,13,11,13)
x1 <- c(-1,-1,-1,-1,1,1,1,1,0,0)
x2 <- c(-1,1,1,1,-1,-1,-1,1,0,0)
m.f <- lm(y~x1+x2)
m.r <- lm(y~factor(x1):factor(x2))
anova(m.f, m.r)
最後一列即为所求。
重点就在於 m.r 的自变项是一个「所有 x1 与 x2 的组合」。
在 R 中的 formula 采取「:」字符有相同功能。
如果不够直白的话,可以看看以下的操作,结果是一样的。
x12 <- paste(x1, x2, sep="&")
m.r1 <- lm(y~factor(x12))
anova(m.f, m.r1)
※ 引述《DrRd (就这样吧)》之铭言:
: [问题类型]:
: 程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
: [软体熟悉度]:
: 入门(写过其他程式,只是对语法不熟悉)
: [问题叙述]:
: 用R来执行多元回归的lack of fit test
: 在网路上找到的方法是单回归用的,多元回归不知道怎麽做
: [程式范例]:
: reduce.mod = lm(y ~ x)
: full.mod = lm(y ~ 0 + as.factor(x))
: anova(reduce.mod, full.mod)
: 但多元回归不能把full.mod写成 lm(y ~ 0 + as.factor(x1) + as.factor(x2))
: 这样出来的自由度是错的
: [关键字]:
:
: lack of fit
:
--
http://apansharing.blogspot.com/
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 122.117.37.172
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/R_Language/M.1398852025.A.D35.html
※ 编辑: andrew43 (122.117.37.172), 04/30/2014 18:00:39
1F:推 DrRd:果真如此!感恩感谢! 04/30 22:01
※ 编辑: andrew43 (122.117.37.172), 04/30/2014 22:14:47