找回密码
 注册
查看: 3774|回复: 0

R语言:predict.gam()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 21:37:05 | 显示全部楼层 |阅读模式
predict.gam(mgcv)
predict.gam()所属R语言包:mgcv

                                        Prediction from fitted GAM model
                                         从拟合的GAM模型预测

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Takes a fitted gam object produced by gam()  and produces predictions given a new set of values for the model covariates  or the original values used for the model fit. Predictions can be accompanied by standard errors, based on the posterior distribution of the model coefficients. The routine can optionally return the matrix by which the model coefficients must be pre-multiplied in order to yield the values of the linear predictor at the supplied covariate values: this is useful for obtaining credible regions for quantities derived from the model (e.g. derivatives of smooths), and for lookup table prediction outside
注意到一个装有gam gam(),并产生一套新的模型协变量的原始值或用于模型拟合值的预测产生的对象。基于模型系数的后验分布的标准误差,可伴有预测。例程可以选择返回该模型系数必须预先以产生协提供的值的线性预测值相乘的矩阵:这是获得可信的区域,从模型中派生的数量(如衍生工具平滑),查找表预测外


用法----------Usage----------


## S3 method for class 'gam'
predict(object,newdata,type="link",se.fit=FALSE,terms=NULL,
        block.size=1000,newdata.guaranteed=FALSE,na.action=na.pass,...)



参数----------Arguments----------

参数:object
a fitted gam object as produced by gam().  
装gam对象由gam()生产。


参数:newdata
A data frame or list containing the values of the model covariates at which predictions are required. If this is not provided then predictions corresponding to the original data are returned. If newdata is provided then it should contain all the variables needed for prediction: a warning is generated if not.   
一个数据框或模型的值列表,其中包含协变量预测。如果不这样,然后提供相应的原始数据的预测被返回。如果newdata提供了,那么它应包含所有需要的变量预测:如果不产生警告。


参数:type
When this has the value "link" (default) the linear predictor (possibly with associated standard errors) is returned. When type="terms" each component of the  linear predictor is returned seperately (possibly with standard errors): this includes  parametric model components, followed by each smooth component, but excludes any offset and any intercept. type="iterms" is the same, except that any standard errors  returned for smooth components will include the uncertainty about the intercept/overall mean.  When  type="response" predictions  on the scale of the response are returned (possibly with approximate standard errors). When type="lpmatrix" then a matrix is returned which yields the values of the linear predictor (minus any offset) when postmultiplied by the parameter vector (in this case se.fit is ignored). The latter option is most useful for getting variance estimates for quantities derived from the model: for example integrated quantities, or derivatives of smooths. A linear predictor matrix can also be used to implement approximate prediction outside R (see example code, below).  
当这个值"link"(默认)的线性预测(可能与相关的标准误差)返回。当type="terms"返回的线性预测的每个组件是分开(可能与标准误差):这包括参数的模型组件,每个平稳组件,但不包括任何偏移和任何拦截。 type="iterms"是相同的,除了标准顺利组件返回任何错误,将包括有关拦截/整体平均的不确定性。当type="response"返回响应规模的预测(可能近似的标准误差)。当type="lpmatrix"postmultiplied参数矢量(在这种情况下se.fit被忽略)时产生的线性预测值(减去任何偏移),然后返回一个矩阵。后者的选项是从模型中派生的数量越来越方差估计最有用的,例如集成的数量,或衍生工具的平滑。线性预测矩阵,也可以被用来实现近似预测外R(见示例代码,下同)。


参数:se.fit
when this is TRUE (not default) standard error estimates are returned for each prediction.
这是真的(不是默认)返回的每个预测标准误差估计。


参数:terms
if type=="terms" then only results for the terms given in this array will be returned.
如果type=="terms"然后将返回结果仅此数组中给定的条款。


参数:block.size
maximum number of predictions to process per call to underlying code: larger is quicker, but more memory intensive. Set to < 1 to use total number of predictions as this.
预测每调用底层的代码来处理的最大数量:较大的是更快,但更多的内存密集型。设置<1使用总数的预测。


参数:newdata.guaranteed
Set to TRUE to turn off all checking of newdata except for sanity of factor levels: this can speed things up for large prediction tasks, but newdata must be complete, with no NA values for predictors required in the model.  
设置TRUE关闭所有检查newdata理智因子水平:这样可以加快大预测任务,但newdata不<X必须是完整的, >需要在模型的预测值。


参数:na.action
what to do about NA values in newdata. With the default na.pass, any row of newdata containing NA values for required predictors, gives rise to NA predictions (even if the term concerned has no NA predictors). na.exclude or na.omit result in the dropping of newdata rows, if they contain any NA values for required predictors. If newdata is missing then NA handling is  determined from object$na.action.
该怎么做NA值newdata。默认na.pass,任何行newdataNA值包含所需的预测,导致NA预测(即使长期关注有没有NA预测)。 na.exclude或na.omit滴newdata行的结果,如果它们包含任何NA所需的预测值。如果newdata缺少NA处理决定从object$na.action。


参数:...
other arguments.
其他参数。


Details

详情----------Details----------

The standard errors produced by predict.gam are based on the Bayesian posterior covariance matrix of the parameters Vp in the fitted gam object.
predict.gam产生的标准误差参数的贝叶斯后验协方差矩阵为基础的Vp拟合GAM对象。

To facilitate plotting with termplot, if object possesses an attribute "para.only" and type=="terms" then only parametric terms of order 1 are returned (i.e. those that termplot can handle).
为了便于策划与termplot如果object拥有一个属性"para.only"和type=="terms"然后只有1阶参数方面则返回(即那些termplot可以处理)。

Note that, in common with other prediction functions, any offset supplied to gam as an argument is always ignored when predicting, unlike offsets specified in the gam model formula.
请注意,在共同与其他预测功能,提供任何偏移gam作为一个参数预测不像GAM模型公式中指定的偏移时,总是被忽略。

See the examples for how to use the lpmatrix for obtaining credible regions for quantities derived from the model.
如何使用从模型中派生的数量获得可信的区域lpmatrix的例子。


值----------Value----------

If type=="lpmatrix" then a matrix is returned which will give a vector of linear predictor values (minus any offest) at the supplied covariate values, when applied to the model coefficient vector.  Otherwise, if se.fit is TRUE then a 2 item list is returned with items (both arrays) fit and se.fit containing predictions and associated standard error estimates, otherwise an  array of predictions is returned. The dimensions of the returned arrays depends on whether  type is "terms" or not: if it is then the array is 2 dimensional with each  term in the linear predictor separate, otherwise the array is 1 dimensional and contains the  linear predictor/predicted values (or corresponding s.e.s). The linear predictor returned termwise will  not include the offset or the intercept.
如果type=="lpmatrix"然后返回一个矩阵,这将给一个线性预测值向量(减去offest),在协提供的价值,当应用到模型系数向量。否则,如果se.fit是TRUE然后2项目列表项目(两个数组)返回fit和se.fit含预测和相关标准的错误估计,否则阵列预测返回。返回的数组的尺寸取决于是否type是"terms"或不是:如果是阵列中的每个单独的线性预测的任期为2维,否则数组是一维的,并且包含线性预测/预测值(或相应的SES)。返回线性预测逐项将不包括偏移或拦截。

newdata can be a data frame, list or model.frame: if it's a model frame then all variables must be supplied.
newdata可以是一个数据框,列表或model.frame:如果它是一个模型框架,那么必须提供所有的变量。


警告----------WARNING ----------

Note that the behaviour of this function is not identical to  predict.gam() in Splus.
请注意,这个函数的行为是不相同的predict.gam()在Splus。

type=="terms" does not exactly match what predict.lm does for parametric model components.
type=="terms"不完全匹配什么predict.lm做参数模型元件。


作者(S)----------Author(s)----------


Simon N. Wood <a href="mailto:simon.wood@r-project.org">simon.wood@r-project.org</a>

The design is inspired by the S function of the same name described in
Chambers and Hastie (1993) (but is not a clone).




参考文献----------References----------


and Hall/CRC Press.

参见----------See Also----------

gam, gamm, plot.gam
gam,gamm,plot.gam


举例----------Examples----------


library(mgcv)
n<-200
sig <- 2
dat <- gamSim(1,n=n,scale=sig)

b<-gam(y~s(x0)+s(I(x1^2))+s(x2)+offset(x3),data=dat)

newd <- data.frame(x0=(0:30)/30,x1=(0:30)/30,x2=(0:30)/30,x3=(0:30)/30)
pred <- predict.gam(b,newd)

#############################################[############################################]
## difference between "terms" and "iterms"[#的区别“条件”和“iterms”]
#############################################[############################################]
nd2 <- data.frame(x0=c(.25,.5),x1=c(.25,.5),x2=c(.25,.5),x3=c(.25,.5))
predict(b,nd2,type="terms",se=TRUE)
predict(b,nd2,type="iterms",se=TRUE)

#########################################################[################################################## ######]
## now get variance of sum of predictions using lpmatrix[#现在方差总和使用lpmatrix的预测]
#########################################################[################################################## ######]

Xp <- predict(b,newd,type="lpmatrix")

## Xp %*% coef(b) yields vector of predictions[XP#%*%系数(B)产量预测的矢量]

a <- rep(1,31)
Xs &lt;- t(a) %*% Xp ## Xs %*% coef(b) gives sum of predictions[#XS%*%系数(B)给出了预测的总和]
var.sum <- Xs %*% b$Vp %*% t(Xs)


#############################################################[################################################## ##########]
## Now get the variance of non-linear function of predictions[#现在,非线性预测功能的变化]
## by simulation from posterior distribution of the params[#从模拟后验分布的params]
#############################################################[################################################## ##########]

rmvn &lt;- function(n,mu,sig) { ## MVN random deviates[#MVN的随机偏离]
  L <- mroot(sig);m <- ncol(L);
  t(mu + L%*%matrix(rnorm(m*n),m,n))
}

br &lt;- rmvn(1000,coef(b),b$Vp) ## 1000 replicate param. vectors[#1000复制参数。向量]
res <- rep(0,1000)
for (i in 1:1000)
{ pr &lt;- Xp %*% br[i,] ## replicate predictions[#复制预测]
  res[i] &lt;- sum(log(abs(pr))) ## example non-linear function[#例如非线性函数]
}
mean(res);var(res)

## loop is replace-able by following .... [#循环更换后....]

res <- colSums(log(abs(Xp %*% t(br))))

##################################################################[################################################## ###############]
## The following shows how to use use an "lpmatrix" as a lookup [#下面显示了如何使用查找使用“lpmatrix”]
## table for approximate prediction. The idea is to create [#近似预测表。我们的想法是创建]
## approximate prediction matrix rows by appropriate linear [#近似的预测,由相应的线性矩阵行]
## interpolation of an existing prediction matrix. The additivity [#插值现有的预测矩阵。可加]
## of a GAM makes this possible. [自由亚齐运动#使这成为可能。]
## There is no reason to ever do this in R, but the following [#没有理由永远做研发,但以下]
## code provides a useful template for predicting from a fitted [#代码提供了一个有用的预测从装模板]
## gam *outside* R: all that is needed is the coefficient vector [#GAM *外* R;:所有需要的是系数向量]
## and the prediction matrix. Use larger `Xp'/ smaller `dx' and/or [#和预测矩阵。使用较大的XP/小DX和/或]
## higher order interpolation for higher accuracy.  [#更高的精度更高的阶插值。]
###################################################################[################################################## ################]

xn &lt;- c(.341,.122,.476,.981) ## want prediction at these values[#希望在这些值的预测]
x0 &lt;- 1         ## intercept column[#拦截列]
dx &lt;- 1/30      ## covariate spacing in `newd'[#协间距NEWD“]
for (j in 0:2) { ## loop through smooth terms[#顺利条款遍历]
  cols &lt;- 1+j*9 +1:9      ## relevant cols of Xp[#有关COLS的XP]
  i &lt;- floor(xn[j+1]*30)  ## find relevant rows of Xp[#找到XP的相关行]
  w1 &lt;- (xn[j+1]-i*dx)/dx ## interpolation weights[#插值权]
  ## find approx. predict matrix row portion, by interpolation[#找到约。预测矩阵行的一部分,通过插值]
  x0 <- c(x0,Xp[i+2,cols]*w1 + Xp[i+1,cols]*(1-w1))
}
dim(x0)<-c(1,28)
fv &lt;- x0%*%coef(b) + xn[4];fv    ## evaluate and add offset[#评估和添加偏移]
se &lt;- sqrt(x0%*%b$Vp%*%t(x0));se ## get standard error[#标准错误。]
## compare to normal prediction[#比较正常的预测]
predict(b,newdata=data.frame(x0=xn[1],x1=xn[2],
        x2=xn[3],x3=xn[4]),se=TRUE)

####################################################################[################################################## #################]
## Differentiating the smooths in a model (with CIs for derivatives)[#判别模型中的平滑(CIS衍生工具)]
####################################################################[################################################## #################]

## simulate data and fit model...[#模拟数据和合适的模型......]
dat <- gamSim(1,n=300,scale=sig)
b<-gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
plot(b,pages=1)

## now evaluate derivatives of smooths with associated standard [#现在评估与相关标准的衍生工具的平滑]
## errors, by finite differencing...[#错误,有限差分...]
x.mesh &lt;- seq(0,1,length=200) ## where to evaluate derivatives[#在评估衍生工具]
newd <- data.frame(x0 = x.mesh,x1 = x.mesh, x2=x.mesh,x3=x.mesh)
X0 <- predict(b,newd,type="lpmatrix")

eps &lt;- 1e-7 ## finite difference interval[#有限差分间隔]
x.mesh &lt;- x.mesh + eps ## shift the evaluation mesh[#转向评价网]
newd <- data.frame(x0 = x.mesh,x1 = x.mesh, x2=x.mesh,x3=x.mesh)
X1 <- predict(b,newd,type="lpmatrix")

Xp &lt;- (X1-X0)/eps ## maps coefficients to (fd approx.) derivatives[#地图系数(FD约)。衍生物]
colnames(Xp)      ## can check which cols relate to which smooth[#可以检查其中COLS涉及到它的顺利]

par(mfrow=c(2,2))
for (i in 1:4) {  ## plot derivatives and corresponding CIs[#剧情衍生工具和相应的负债证明书]
  Xi <- Xp*0
  Xi[,(i-1)*9+1:9+1] &lt;- Xp[,(i-1)*9+1:9+1] ## Xi%*%coef(b) = smooth deriv i[#熙%*%系数(B)=我顺利DERIV]
  df &lt;- Xi%*%coef(b)              ## ith smooth derivative [#i个顺利衍生]
  df.sd &lt;- rowSums(Xi%*%b$Vp*Xi)^.5 ## cheap diag(Xi%*%b$Vp%*%t(Xi))^.5[#廉价的诊断(十一%*%B $ VP%*%T(西))^ 0.5]
  plot(x.mesh,df,type="l",ylim=range(c(df+2*df.sd,df-2*df.sd)))
  lines(x.mesh,df+2*df.sd,lty=2);lines(x.mesh,df-2*df.sd,lty=2)
}




转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-23 15:09 , Processed in 0.024378 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表