本站分享:AI、大数据、数据分析师培训认证考试,包括:Python培训Excel培训Matlab培训SPSS培训SAS培训R语言培训Hadoop培训Amos培训Stata培训Eviews培训

r语言predict函数_R语言:predict.lm()函数中文帮助文档(中英文对照)

r语言 cdadata 27223℃

r语言predict函数

在predict函数中,
predict (object, …)result1<-predict(result,newdata,interval=”confidence”)这里面的newdata有什么要求呢,必须是解释变量里面的值吗,随便带入值不可以预测吗?


解答:

可以。但newdata似乎要求是数据框,并且数据框里每个变量的名字要与原先模型里的一致。

要和原始数据一样的数据类型,以data.frame()的形式带入predict()

如果你的模型是y~x,(x是自变量),首先估计模型
Model=lm(y~x,data=…)
估计出来后,进行预测,例如预测x=2时候的y值,则应当输入命令
predict(Model,newdata=data.frame(x=2),interval=”confidence”)


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

predict.lm(stats)
predict.lm()所属R语言包:stats

                                        Predict method for Linear Model Fits
                                         对于线性模型拟合预测方法

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

描述———-Description———-
Predicted values based on linear model object.
预测值基于线性模型对象。

用法———-Usage———-

## S3 method for class ‘lm’
predict(object, newdata, se.fit = FALSE, scale = NULL, df = Inf,
        interval = c(“none”, “confidence”, “prediction”),
        level = 0.95, type = c(“response”, “terms”),
        terms = NULL, na.action = na.pass,
        pred.var = res.var/weights, weights = 1, …)

参数———-Arguments———-

参数:object
Object of class inheriting from “lm”
继承类的对象从”lm”

参数:newdata
An optional data frame in which to look for variables with which to predict.  If omitted, the fitted values are used.
一个可选的数据框寻找与预测的变数。如果省略,用来拟合值。

参数:se.fit
A switch indicating if standard errors are required.
一个开关,如果需要标准误差。

参数:scale
Scale parameter for std.err. calculation
尺度参数std.err。计算

参数:df
Degrees of freedom for scale
度规模的自由

参数:interval
Type of interval calculation.
区间计算的类型。

参数:level
Tolerance/confidence level
宽容/置信水平

参数:type
Type of prediction (response or model term).
预测型(反应或模型长期)。

参数:terms
If type=”terms”, which terms (default is all terms)
如果type=”terms”,哪些条款(默认是所有条款)

参数:na.action
function determining what should be done with missing values in newdata.  The default is to predict NA.
功能确定应做与newdata缺失值。默认预测NA。

参数:pred.var
the variance(s) for future observations to be assumed for prediction intervals.  See “Details”.
要承担未来的观测方差(S)的预测区间。见“详细资料”。

参数:weights
variance weights for prediction. This can be a numeric vector or a one-sided model formula. In the latter case, it is interpreted as an expression evaluated in newdata
预测方差权。这可以是一个数值向量或片面的模型公式。在后一种情况下,它被解释为表达newdata评价

参数:…
further arguments passed to or from other methods.
通过进一步的论据或其他方法。

Details

详情———-Details———-

predict.lm produces predicted values, obtained by evaluating the regression function in the frame newdata (which defaults to model.frame(object).  If the logical se.fit is TRUE, standard errors of the predictions are calculated.  If the numeric argument scale is set (with optional df), it is used as the residual standard deviation in the computation of the standard errors, otherwise this is extracted from the model fit. Setting intervals specifies computation of confidence or prediction (tolerance) intervals at the specified level, sometimes referred to as narrow vs. wide intervals.
predict.lm生产预测值,取得通过评估的回归函数框架newdata(默认为model.frame(object)。如果逻辑se.fit是TRUE,标准误差预测计算,如果数字参数scale设置(可选df),它被用于残留在计算标准误差的标准偏差,否则这是从模型中提取适合设置intervals指定的信心,或在指定level,有时简称为窄与宽间隔的预测(耐)间隔的计算。

If the fit is rank-deficient, some of the columns of the design matrix will have been dropped.  Prediction from such a fit only makes sense if newdata is contained in the same subspace as the original data.  That cannot be checked accurately, so a warning is issued.
如果缺乏合适的是排名,一些设计矩阵的列将被丢弃。从这样一个合适的预测才有意义newdata如果包含在原始数据相同的子空间。不能准确地检查,所以会发出一个警告。

If newdata is omitted the predictions are based on the data used for the fit.  In that case how cases with missing values in the original fit is determined by the na.action argument of that fit.  If na.action = na.omit omitted cases will not appear in the residuals, whereas if na.action = na.exclude they will appear (in predictions, standard errors or interval limits), with residual value NA.  See also napredict.
newdata如果省略的预测是基于适合使用的数据。在这种情况下,如何在原来的拟合遗漏值的情况下确定na.action合身的说法。如果na.action = na.omit省略的情况下将不会出现在残差,而剩余价值na.action = na.exclude如果NA“他们会出现预测,标准错误或间隔限制,。还可以看napredict。

The prediction intervals are for a single observation at each case in newdata (or by default, the data used for the fit) with error variance(s) pred.var. This can be a multiple of res.var, the estimated value of &sigma;^2: the default is to assume that future observations have the same error variance as those used for fitting. If weights is supplied, the inverse of this is used as a scale factor. For a weighted fit, if the prediction is for the original data frame, weights defaults to the weights used for the  model fit, with a warning since it might not be the intended result. If the fit was weighted and newdata is given, the default is to assume constant prediction variance, with a warning.
预测区间在每一种情况下的单一观察newdata(或默认情况下,使用的数据为合适)与误差方差()pred.var的。这可以是1res.var,估计价值&sigma;^2多:默认是假设未来的观测装修使用的相同的误差方差。如果weights提供,这反被用来作为一个尺度因子。加权拟合,如果预测是原始的数据框,weights默认用于模型拟合的权重警告,因为它可能无法预期的结果。如果合适的加权和newdata,默认是假设恒定的预测方差,一个警告。

值———-Value———-

predict.lm produces a vector of predictions or a matrix of predictions and bounds with column names fit, lwr, and upr if interval is set.  If se.fit is TRUE, a list with the following components is returned:
predict.lm生产fit,lwr,upr如果interval设置的预测向量或矩阵的列名的预测与边界。如果se.fitTRUE,以下组件的列表,则返回:

参数:fit
vector or matrix as above
上述的向量或矩阵

参数:se.fit
standard error of predicted means
预测方法的标准误差

参数:residual.scale
residual standard deviations
剩余标准偏差

参数:df
degrees of freedom for residual
剩余的自由度

注意———-Note———-
Variables are first looked for in newdata and then searched for in the usual way (which will include the environment of the formula used in the fit).  A warning will be given if the variables found are not of the same length as those in newdata if it was supplied.
变量在newdata先看着,然后在通常的方法(其中包括环境适合使用的公式)搜查。一个将给予警告,如果发现的变量是相同长度不为那些在newdata如果它提供的。

Notice that prediction variances and prediction intervals always refer to future observations, possibly corresponding to the same predictors as used for the fit. The variance of the residuals will be smaller.
请注意,预测方差和预测区间总是指未来的观测,可能适合使用相同的预测相应。残差的方差较小。

Strictly speaking, the formula used for prediction limits assumes that the degrees of freedom for the fit are the same as those for the residual variance.  This may not be the case if res.var is not obtained from the fit.
严格地说,用于预测限制的公式假设,自由的契合度是相同的残差。这可能不是res.var如果不适合获得的情况下。

参见———-See Also———-
The model fitting function lm, predict.
该模型的拟合函数lm,predict。

SafePrediction for prediction from polynomial and spline fits.
SafePrediction多项式样条拟合预测。

举例———-Examples———-

require(graphics)

## Predictions[#预测]
x <- rnorm(15)
y <- x + rnorm(15)
predict(lm(y ~ x))
new <- data.frame(x = seq(-3, 3, 0.5))
predict(lm(y ~ x), new, se.fit = TRUE)
pred.w.plim <- predict(lm(y ~ x), new, interval=”prediction”)
pred.w.clim <- predict(lm(y ~ x), new, interval=”confidence”)
matplot(new$x,cbind(pred.w.clim, pred.w.plim[,-1]),
        lty=c(1,2,2,3,3), type=”l”, ylab=”predicted y”)

## Prediction intervals, special cases[预测区间,特殊情况下]
##  The first three of these throw warnings[#这些罚球警告前三]
w <- 1 + x^2
fit <- lm(y ~ x)
wfit <- lm(y ~ x, weights = w)
predict(fit, interval = “prediction”)
predict(wfit, interval = “prediction”)
predict(wfit, new, interval = “prediction”)
predict(wfit, new, interval = “prediction”, weights = (new$x)^2)
predict(wfit, new, interval = “prediction”, weights = ~x^2)

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

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

转载请注明:数据分析 » r语言predict函数_R语言:predict.lm()函数中文帮助文档(中英文对照)

喜欢 (12)or分享 (0)