newdata(rockchalk)
newdata()所属R语言包:rockchalk
Creates the newdata frame required in predict.
创建newdata在预测需要。
译者:生物统计家园网 机器人LoveR
描述----------Description----------
If not supplied with a focus list, newdata returns a data frame with one row– the central values (means and modes) of the variables in the data that was used to fit the model. To declare some variables that the user wants to focus on, the user should supply a fitted model "model" and a focus list "fl" of variable values. The fl list must be a named list, using names of variables from the regression formula. It is not needed to call this directly if one is satisfied with the results from predictOMatic.
如果没有提供一个关注名单,newdata一个行的核心价值(手段和方式)的数据被用来拟合模型中的变量返回一个数据框。要声明的用户希望把重点放在一些变量,用户应提供一个合适的模型“模型”和“FL”的焦点列表的变量值。佛罗里达州列表必须是命名列表,使用变量的回归公式的名称。它不需要调用此,直接一个满意的结果predictOMatic。
用法----------Usage----------
newdata(model = NULL, fl = NULL, emf = NULL)
参数----------Arguments----------
参数:model
Required. Fitted regression model
必需的。拟合回归模型
参数:fl
Optional. "focus list" of variables. Named list of variables and values for which to create a new data object.
可选。 “关注名单”的变量。命名变量和值创建一个新的数据对象列表。
参数:emf
Optional. data frame used to fit model (not a model frame, which may include transformed variables like log(x1). Instead, use output from function model.data). It is UNTRANSFORMED variables ("x" as opposed to poly(x,2).1 and poly(x,2).2).
可选。数据框用来拟合模型(而不是模型框架,其中可能包括像log(X1)转换的变量。相反,使用输出函数model.data)。它是未转化的变量(“x”作为相对聚(的x,2)0.1和聚(的x,2),0.2)。
值----------Value----------
A data frame of x values that could be used as the data= argument in the original regression model. The attribute "varNamesRHS" is a vector of the predictor values.
一个数据框的数据在原来的回归模型的参数,可以用来作为x的值。的属性“varNamesRHS”是一个矢量的预测值。
(作者)----------Author(s)----------
Paul E. Johnson <pauljohn@ku.edu>
参见----------See Also----------
predictOMatic
predictOMatic
实例----------Examples----------
library(rockchalk)
set.seed(12345)
N <- 100
x1 <- rpois(N, l=6)
x2 <- rnorm(N, m=50, s=10)
x3 <- rnorm(N)
xcat1 <- gl(2,50, labels=c("M","F"))
xcat2 <- cut(rnorm(N), breaks=c(-Inf, 0, 0.4, 0.9, 1, Inf), labels=c("R", "M", "D", "P", "G"))
dat <- data.frame(x1, x2, x3, xcat1, xcat2)
rm(x1, x2, x3, xcat1, xcat2)
xcat1n <- with(dat, contrasts(xcat1)[xcat1, ,drop=FALSE])
xcat2n <- with(dat, contrasts(xcat2)[xcat2, ])
STDE <- 15
dat$y <- 0.03 + 0.8*dat$x1 + 0.1*dat$x2 + 0.7*dat$x3 + xcat1n %*% c(2) + xcat2n %*% c(0.1,-2,0.3, 0.1) + STDE*rnorm(N)
rownames(dat$y) <- NULL
## rownames of dat$y don't match rownames of dat. Humphf.[#DAT $ Y不匹配的行名行名的DAT。 Humphf。]
## I've not seen that problem before[#我从来没见过这个问题之前]
dat$x1[sample(N, 5)] <- NA
dat$x2[sample(N, 5)] <- NA
dat$x3[sample(N, 5)] <- NA
dat$xcat2[sample(N, 5)] <- NA
dat$xcat1[sample(N, 5)] <- NA
dat$y[sample(N, 5)] <- NA
m0 <- lm(y ~ x1 + x2 + xcat1, data = dat)
summary(m0)
m0.data <- model.data(m0)
summarize(m0.data)
(m0.p1 <- predictOMatic(m0))
(m0.p2 <- predictOMatic(m0, interval = "confidence"))
(m0.p3 <- predictOMatic(m0, divider="std.dev.", n=5))
(m0.p3 <- predictOMatic(m0, fl = list("x1" = c(6,7), "xcat1" = levels(m0.data$xcat1))))
m1 <- lm(y ~ log(10+x1) + sin(x2) + x3, data=dat)
m1.data <- model.data(m1)
summarize(m1.data)
(newdata(m1))
(newdata(m1, fl = list(x1=c(6, 8, 10))))
(newdata(m1, fl = list(x1 = c(6, 8, 10), x3 = c(-1,0,1))))
(newdata(m1, fl = list(x1 = c(6, 8, 10), x2 = quantile(m1.data$x2), x3 = c(-1,0,1))))
(m1.p1 <- predictOMatic(m1, divider="std.dev", n = 5))
(m1.p1 <- predictOMatic(m1, divider="quantile", n = 5))
(m1.p1 <- predictOMatic(m1, fl=list(x1=c(6, 8, 10), x2 = median(m1.data$x2, na.rm = TRUE))))
(m1.p1 <- predictOMatic(m1, fl=list(x1=c(6, 8, 10), x2 = quantile(m1.data$x2, na.rm = TRUE))))
(m1.p1 <- predictOMatic(m1))
(m1.p1 <- predictOMatic(m1, divider="std.dev."))
(m1.p1 <- predictOMatic(m1, divider="std.dev.", n=5))
(m1.p1 <- predictOMatic(m1, divider="std.dev.", interval="confidence"))
m2 <- lm(y ~ x1 + x2 + x3 + xcat1 + xcat2, data = dat)
## has only columns and rows used in model fit[#只有列和行中使用的模型拟合]
m2.data <- model.data(m2)
summarize(m2.data)
newdata(m2, fl = list(xcat1 = levels(m2.data$xcat1)))
## mix and match all combinations of xcat1 and xcat2[#混合和匹配的所有组合xcat1和xcat2]
newdata(m2, fl = list(xcat1 = levels(m2.data$xcat1), xcat2 = levels(m2.data$xcat2)))
m2.new1 <- newdata(m2, fl = list(xcat1 = levels(m2.data$xcat1), xcat2 = levels(m2.data$xcat2)))
predict(m2, newdata = m2.new1)
## Pick some particular values for focus[#为重点挑选一些特定的值]
m2.new2 <- newdata(m2, fl = list(x1 = c(1,2,3), xcat2 = c("M","D")))
## Ask for predictions[#要求预测]
predict(m2, newdata = m2.new2)
## Compare: predictOMatic generates a newdata frame and predictions in one step[#比较:predictOMatic的生成newdata在一个步骤的框架和预测]
(m2.p1 <- predictOMatic(m2, fl = list(xcat1 = levels(m2.data$xcat1), xcat2 = levels(m2.data$xcat2))))
(m2.p2 <- predictOMatic(m2, fl = list(x1 = c(1,2,3), xcat2 = c("M","D"))))
(m2.p3 <- predictOMatic(m2, fl = list(x2 = c(0.25, 1.0), xcat2 = c("M","D"))))
(m2.p4 <- predictOMatic(m2, fl = list(x2 = plotSeq(m2.data$x2, 10) , xcat2 = c("M","D"))))
(m2.p5 <- predictOMatic(m2, fl = list(x2 = c(0.25, 1.0), xcat2 = c("M","D")), interval="conf"))
(predictOMatic(m2, interval="conf"))
(m2.p6 <- predictOMatic(m2, fl = list(x2 = c(49, 51), xcat2 = levels(m2.data$xcat2), x1=plotSeq(dat$x1))))
plot(y ~ x1, data= m2.data)
by(m2.p6, list(m2.p6$xcat2), function(x) {lines(x$x1, x$fit, col=x$xcat2, lty=as.numeric(x$xcat2))})
m2.newdata <- newdata(m2, fl = list(x2 = c(48, 50, 52), xcat2 = c("M","D")))
predict(m2, newdata = m2.newdata)
(m2.p7 <- predictOMatic(m2, fl = list(x2 = c(48, 50, 52), xcat2 = c("M","D"))))
(m2.p8 <- predictOMatic(m2, fl = list(x2 = range(m2.data$x2), xcat2 = c("M","D"))))
(m2.p9 <- predictOMatic(m2, fl = list(x2 = plotSeq(m2.data$x2), x1 = quantile(m2.data$x1, pr =c(0.33, 0.66), na.rm = TRUE), xcat2 = c("M","D"))))
plot(y ~ x2 , data = m2.data)
by(m2.p9, list(m2.p9$x1, m2.p9$xcat2), function(x) {lines(x$x2, x$fit)})
(predictOMatic(m2, fl = list(x2 = c(50, 60), xcat2 = c("M","D")), interval="conf"))
## create a dichotomous dependent variable[#建立一个二分因变量]
y2 <- ifelse(rnorm(N) > 0.3, 1, 0)
dat <- cbind(dat, y2)
m3 <- glm(y2 ~ x1 + x2 + x3 + xcat1, data=dat, family=binomial(logit))
summary(m3)
m3.data <- model.data(m3)
summarize(m3.data)
(m3.p1 <- predictOMatic(m3, divider="std.dev.", type="response"))
(m3.p2 <- predictOMatic(m3, fl = list(x2 = c(40, 50, 60), xcat1 = c("M","F")), divider="response", interval="conf"))
## Want a full accounting for each value of x2?[想要一个完整的会计X2的每个值吗?]
(m3.p3 <- predictOMatic(m3, fl = list(x2 = unique(m3.data$x2), xcat1 = c("M","F")), interval="conf", type="response"))
## Would like to write a more beautiful print method[#想写出更漂亮的印花方法]
## for output object, but don't want to obscure structure from user.[#输出对象,但并不想掩盖结构从用户。]
for (i in names(m3.p1)){
dns <- cbind(m3.p1[[i]][i], m3.p1[[i]]$fit)
colnames(dns) <- c(i, "predicted")
print(dns)
}
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|