r语言lm函数可以做非线性回归吗

Python016

r语言lm函数可以做非线性回归吗,第1张

模型拟合

对于人口模型可以采用Logistic增长函数形式,它考虑了初期的指数增长以及总资源的限制。其函数形式如下。

首先载入car包以便读取数据,然后使用nls函数进行建模,其中theta1、theta2、theta3表示三个待估计参数,start设置了参数初始值,设定trace为真以显示迭代过程。nls函数默认采用Gauss-Newton方法寻找极值,迭代过程中第一列为RSS值,后面三列是各参数估计值。然后用summary返回回归结果。

library(car)

pop.mod1 <- nls(population ~ theta1/(1+exp(-(theta2+theta3*year))),start=list(theta1 = 400, theta2 = -49, theta3 = 0.025), data=USPop, trace=T)

summary(pop.mod)

在上面的回归过程中我们直接指定参数初始值,另一种方法是采用搜索策略,首先确定参数取值范围,然后利用nls2包的暴力方法来得到最优参数。但这种方法相当费时。

还有一种更为简便的方法就是采用内置自启动模型(self-starting Models),此时我们只需要指定函数形式,而不需要指定参数初始值。本例的logistic函数所对应的selfstarting函数名为SSlogis

pop.mod2 <- nls(population ~ SSlogis(year,phi1,phi2,phi3),data=USPop)

二、判断拟合效果

非线性回归模型建立后需要判断拟合效果,因为有时候参数最优化过程会捕捉到局部极值点而非全局极值点。最直观的方法是在原始数据点上绘制拟合曲线。

library(ggplot2)

p <- ggplot(USPop,aes(year, population))

以下为该包的帮助文件内容

Dynamic nomogram to visualise statistical models

Description

DynNom is a generic function to display the results of statistical model objects as a dynamic nomogram in an 'RStudio' panel or web browser. DynNom supports a large number of model objects from a variety of packages.

Usage

DynNom(model, data = NULL, clevel = 0.95, m.summary = c("raw", "formatted"),

covariate = c("slider", "numeric"), ptype = c("st", "1-st"),

DNtitle = NULL, DNxlab = NULL, DNylab = NULL, DNlimits = NULL,

KMtitle = NULL, KMxlab = NULL, KMylab = NULL)

DynNom.core(model, data, clevel, m.summary, covariate, DNtitle, DNxlab, DNylab, DNlimits)

DynNom.surv(model, data, clevel, m.summary, covariate,

ptype, DNtitle, DNxlab, DNylab, KMtitle, KMxlab, KMylab)

Arguments

model

an lm, glm, coxph, ols, Glm, lrm, cph, mgcv::gam or gam::gam model objects.

data

a dataframe of the accompanying dataset for the model (if required).

clevel

a confidence level for constructing the confidence interval. If not specified, a 95% level will be used.

m.summary

an option to choose the type of the model output represented in the 'Summary Model' tab. "raw" (the default) returns an unformatted summary of the model"formatted" returns a formatted table of the model summary using stargazer package.

covariate

an option to choose the type of input control widgets used for numeric values. "slider" (the default) picks out sliderInput"numeric" picks out numericInput.

ptype

an option for coxph or cph model objects to choose the type of plot which displays in "Survival plot" tab. "st" (the default) returns plot of estimated survivor probability (S(t)). "1-st" returns plot of estimated failure probability (1-S(t)).

DNtitle

a character vector used as the app's title. If not specified, "Dynamic Nomogram" will be used.

DNxlab

a character vector used as the title for the x-axis in "Graphical Summary" tab. If not specified, "Probability" will be used for logistic model and Cox proportional model objectsor "Response variable" for other model objects.

DNylab

a character vector used as the title for the y-axis in "Graphical Summary" tab (default is NULL).

DNlimits

a vector of 2 numeric values used to set x-axis limits in "Graphical Summary" tab. Note: This also removes the 'Set x-axis ranges' widget in the sidebar panel.

KMtitle

a character vector used as KM plot's title in "Survival plot" tab. If not specified, "Estimated Survival Probability" for ptype = "st" and "Estimated Probability" for ptype = "1-st" will be used.

KMxlab

a character vector used as the title for the x-axis in "Survival plot" tab. If not specified, "Follow Up Time" will be used.

KMylab

a character vector used as the title for the y-axis in "Survival plot" tab. If not specified, "S(t)" for ptype = "st" and "F(t)" for ptype = "1-st" will be used.

Value

A dynamic nomogram in a shiny application providing individual predictions which can be used as a model visualisation or decision-making tools.

The individual predictions with a relative confidence interval are calculated using the predict function, displaying either graphically as an interactive plot in the Graphical Summary tab or a table in the Numerical Summary tab. A table of model output is also available in the Model Summary tab. In the case of the Cox proportional hazards model, an estimated survivor/failure function will be additionally displayed in a new tab.

Please cite as:

Jalali, A., Roshan, D., Alvarez-Iglesias, A., Newell, J. (2019). Visualising statistical models using dynamic nomograms. R package version 5.0.

Author(s)

Amirhossein Jalali, Davood Roshan, Alberto Alvarez-Iglesias, John Newell

Maintainer: Amirhossein Jalali [email protected]

References

Banks, J. 2006. Nomograms. Encyclopedia of Statistical Sciences. 8.

Easy web applications in R. http://shiny.rstudio.com

Frank E Harrell Jr (2017). rms: Regression Modeling Strategies. R package version 4.5-0. https://CRAN.R-project.org/package=rms

See Also

DNbuilder, getpred.DN

for (i in 1:100){

points(1,x[i,1],col="red")

points(2,x[i,2],col="red")

points(3,x[i,3],col="red")

}

我举得例子是一共100行,三个柱子