我正在使用R. 我想使用一个特定的公式来将曲线拟合到我的一个数据集(附件)
> dput(data)
structure(list(Gossypol = c(1036.331811, 4171.427741, 6039.995102,
5909.068158, 4140.242559, 4854.985845, 6982.035521, 6132.876396,
948.2418407, 3618.448997, 3130.376482, 5113.942098, 1180.171957,
1500.863038, 4576.787021, 5629.979049, 3378.151945, 3589.187889,
2508.417927, 1989.576826, 5972.926124, 2867.610671, 450.7205451,
1120.955, 3470.09352, 3575.043632, 2952.931863, 349.0864019,
1013.807628, 910.8879471, 3743.331903, 3350.203452, 592.3403778,
1517.045807, 1504.491931, 3736.144027, 2818.419785, 723.885643,
1782.864308, 1414.161257, 3723.629772, 3747.076592, 2005.919344,
4198.569251, 2228.522959, 3322.115942, 4274.324792, 720.9785449,
2874.651764, 2287.228752, 5654.858696, 1247.806111, 1247.806111,
2547.326207, 2608.716056, 1079.846532), Treatment = structure(c(2L,
3L, 4L, 5L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L,
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 1L), .Label = c("C", "1c_2d", "3c_2d",
"9c_2d", "1c_7d"), class = "factor"), Damage_cm = c(0.4955, 1.516,
4.409, 3.2665, 0.491, 2.3035, 3.51, 1.8115, 0, 0.4435, 1.573,
1.8595, 0, 0.142, 2.171, 4.023, 4.9835, 0, 0.6925, 1.989, 5.683,
3.547, 0, 0.756, 2.129, 9.437, 3.211, 0, 0.578, 2.966, 4.7245,
1.8185, 0, 1.0475, 1.62, 5.568, 9.7455, 0, 0.8295, 2.411, 7.272,
4.516, 0, 0.4035, 2.974, 8.043, 4.809, 0, 0.6965, 1.313, 5.681,
3.474, 0, 0.5895, 2.559, 0)), .Names = c("Gossypol", "Treatment",
"Damage_cm"), row.names = c(NA, -56L), class = "data.frame")
等式为:y~yo+a*(1-b^x)
哪里:
y = Gossypol
(来自我的数据集)
x = Damage_cm
(来自我的数据集)
其他3个参数未知:
yo = Intercept
,a = asymptote
和b = slope
我想我必须使用包nls2
。到目前为止,我编写了以下代码:
data<-read.csv("Regression_exp2.csv",header=T, sep = ",")
library(nls2)
attach(data)
m<-nls(Gossypol~Y+A*(1-B^Damage_cm),data=data,start = list(Y=1700,A=4000,B=1))
这给了我错误信息:
nlsModel中的错误(formula,mf,start,wts):奇异梯度矩阵 在初始参数估计值
最后我想用方程式绘制一条曲线(有SE间隔,我通常使用ggplot2)
此外,我想知道R2和p值。
我也会对参数yo
,a
和b
我之前从未这样做过,如果有人能帮助我或者给我一个提示如何在R中做到这一点,我将非常感激?我想我必须使用非线性方法(glm(...))
非常感谢, 麦克
答案 0 :(得分:1)
您必须稍微调整一下起始值:
TID: [0] [ESB] [2015-08-07 22:13:21,497] DEBUG {org.apache.synapse.transport.http.wire} - >> "POST /internal/employees HTTP/1.1[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,497] DEBUG {org.apache.synapse.transport.http.wire} - >> "Accept-Encoding: gzip,deflate[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,498] DEBUG {org.apache.synapse.transport.http.wire} - >> "Content-Type: application/json[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,498] DEBUG {org.apache.synapse.transport.http.wire} - >> "Content-Length: 70[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,498] DEBUG {org.apache.synapse.transport.http.wire} - >> "Host: 192.168.1.167:8280[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,499] DEBUG {org.apache.synapse.transport.http.wire} - >> "Connection: Keep-Alive[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,499] DEBUG {org.apache.synapse.transport.http.wire} - >> "User-Agent: Apache-HttpClient/4.1.1 (java 1.5)[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,499] DEBUG {org.apache.synapse.transport.http.wire} - >> "[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,500] DEBUG {org.apache.synapse.transport.http.wire} - >> "{"employee":{"firstName":"Jackie","lastName":"Chan","team":"Finance"}}" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,552] INFO {org.apache.synapse.core.axis2.TimeoutHandler} - This engine will expire all callbacks after : 120 seconds, irrespective of the timeout action, after the specified or optional timeout {org.apache.synapse.core.axis2.TimeoutHandler}
TID: [0] [ESB] [2015-08-07 22:13:21,572] DEBUG {org.apache.synapse.transport.http.wire} - << "POST /services/EmployeesDataService/employees HTTP/1.1[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,573] DEBUG {org.apache.synapse.transport.http.wire} - << "Accept-Encoding: gzip,deflate[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,573] DEBUG {org.apache.synapse.transport.http.wire} - << "Content-Type: application/json[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,573] DEBUG {org.apache.synapse.transport.http.wire} - << "SOAPAction: [\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,573] DEBUG {org.apache.synapse.transport.http.wire} - << "Transfer-Encoding: chunked[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,574] DEBUG {org.apache.synapse.transport.http.wire} - << "Host: 192.168.1.167:9773[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,574] DEBUG {org.apache.synapse.transport.http.wire} - << "Connection: Keep-Alive[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,574] DEBUG {org.apache.synapse.transport.http.wire} - << "User-Agent: Synapse-PT-HttpComponents-NIO[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,574] DEBUG {org.apache.synapse.transport.http.wire} - << "[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,575] DEBUG {org.apache.synapse.transport.http.wire} - << "57[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,575] DEBUG {org.apache.synapse.transport.http.wire} - << "{"insert_Employee_operation":{"FirstName":"Jackie","LastName":"Chan","Team":"Finance"}}[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,575] DEBUG {org.apache.synapse.transport.http.wire} - << "0[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,575] DEBUG {org.apache.synapse.transport.http.wire} - << "[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,658] DEBUG {org.apache.synapse.transport.http.wire} - >> "HTTP/1.1 500 Internal Server Error[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,659] DEBUG {org.apache.synapse.transport.http.wire} - >> "Transfer-Encoding: chunked[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,659] DEBUG {org.apache.synapse.transport.http.wire} - >> "Date: Sat, 08 Aug 2015 02:13:21 GMT[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,659] DEBUG {org.apache.synapse.transport.http.wire} - >> "Connection: close[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,659] DEBUG {org.apache.synapse.transport.http.wire} - >> "Server: WSO2 Carbon Server[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,660] DEBUG {org.apache.synapse.transport.http.wire} - >> "[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,660] DEBUG {org.apache.synapse.transport.http.wire} - >> "114[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,660] DEBUG {org.apache.synapse.transport.http.wire} - >> "{"Fault":{"faultcode":"axis2ns1:Client","faultstring":"The endpoint reference (EPR) for the Operation not found is /services/EmployeesDataService/employees and the WSA Action = null. If this EPR was previously reachable, please contact the server administrator.","detail":""}}[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,666] DEBUG {org.apache.synapse.transport.http.wire} - >> "0[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,666] DEBUG {org.apache.synapse.transport.http.wire} - >> "[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,672] ERROR {org.apache.synapse.util.xpath.SynapseJsonPath} - #stringValueOf. Error evaluating JSON Path <$.GeneratedKeys.Entry.ID>. Returning empty result. Error>>> invalid path {org.apache.synapse.util.xpath.SynapseJsonPath}
TID: [0] [ESB] [2015-08-07 22:13:21,674] DEBUG {org.apache.synapse.transport.http.wire} - << "HTTP/1.1 500 Internal Server Error[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,674] DEBUG {org.apache.synapse.transport.http.wire} - << "Content-Type: application/json[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,675] DEBUG {org.apache.synapse.transport.http.wire} - << "Date: Sat, 08 Aug 2015 02:13:21 GMT[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,675] DEBUG {org.apache.synapse.transport.http.wire} - << "Server: WSO2-PassThrough-HTTP[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,675] DEBUG {org.apache.synapse.transport.http.wire} - << "Transfer-Encoding: chunked[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,675] DEBUG {org.apache.synapse.transport.http.wire} - << "Connection: Keep-Alive[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,675] DEBUG {org.apache.synapse.transport.http.wire} - << "[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,676] DEBUG {org.apache.synapse.transport.http.wire} - << "43[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,677] DEBUG {org.apache.synapse.transport.http.wire} - << "{"EmployeeRecord":{"EmployeeID":, "Status":"Successfully created"}}[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,677] DEBUG {org.apache.synapse.transport.http.wire} - << "0[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:13:21,677] DEBUG {org.apache.synapse.transport.http.wire} - << "[\r][\n]" {org.apache.synapse.transport.http.wire}
TID: [0] [ESB] [2015-08-07 22:14:56,503] INFO {org.wso2.carbon.core.init.CarbonServerManager} - Shutdown hook triggered.... {org.wso2.carbon.core.init.CarbonServerManager}
TID: [0] [ESB] [2015-08-07 22:14:56,504] INFO {org.wso2.carbon.core.init.CarbonServerManager} - Gracefully shutting down WSO2 Enterprise Service Bus... {org.wso2.carbon.core.init.CarbonServerManager}
TID: [0] [ESB] [2015-08-07 22:14:56,506] INFO {org.wso2.carbon.core.ServerManagement} - Starting to switch to maintenance mode... {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,506] INFO {org.apache.synapse.transport.passthru.PassThroughHttpSSLListener} - Stopping Pass-through HTTPS Listener.. {org.apache.synapse.transport.passthru.PassThroughHttpSSLListener}
TID: [0] [ESB] [2015-08-07 22:14:56,508] INFO {org.apache.synapse.transport.passthru.PassThroughHttpListener} - Stopping Pass-through HTTP Listener.. {org.apache.synapse.transport.passthru.PassThroughHttpListener}
TID: [0] [ESB] [2015-08-07 22:14:56,508] INFO {org.apache.synapse.transport.passthru.PassThroughHttpSSLListener} - HTTPS Listener shutdown. {org.apache.synapse.transport.passthru.PassThroughHttpSSLListener}
TID: [0] [ESB] [2015-08-07 22:14:56,509] INFO {org.apache.synapse.transport.passthru.PassThroughHttpListener} - HTTP Listener shutdown. {org.apache.synapse.transport.passthru.PassThroughHttpListener}
TID: [0] [ESB] [2015-08-07 22:14:56,509] INFO {org.wso2.carbon.core.ServerManagement} - Stopped all transport listeners {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,510] INFO {org.wso2.carbon.core.ServerManagement} - Waiting for request service completion... {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,511] INFO {org.wso2.carbon.core.ServerManagement} - All requests have been served. {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,512] INFO {org.wso2.carbon.core.ServerManagement} - Waiting for deployment completion... {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,512] INFO {org.wso2.carbon.core.ServerManagement} - All deployment tasks have been completed. {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,513] INFO {org.wso2.carbon.core.ServerManagement} - Waiting for server task completion... {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,514] INFO {org.wso2.carbon.core.ServerManagement} - All server tasks have been completed. {org.wso2.carbon.core.ServerManagement}
TID: [0] [ESB] [2015-08-07 22:14:56,514] INFO {org.wso2.carbon.core.init.CarbonServerManager} - Shutting down WSO2 Enterprise Service Bus... {org.wso2.carbon.core.init.CarbonServerManager}
TID: [0] [ESB] [2015-08-07 22:14:56,514] INFO {org.wso2.carbon.core.init.CarbonServerManager} - Shutting down OSGi framework... {org.wso2.carbon.core.init.CarbonServerManager}
TID: [0] [ESB] [2015-08-07 22:14:56,563] INFO {org.apache.synapse.task.SynapseTaskManager} - Shutting down the task manager {org.apache.synapse.task.SynapseTaskManager}
TID: [0] [ESB] [2015-08-07 22:14:58,709] INFO {org.wso2.carbon.core.init.CarbonServerManager} - Stopping CarbonServerManager... {org.wso2.carbon.core.init.CarbonServerManager}
TID: [0] [ESB] [2015-08-07 22:14:58,711] INFO {org.apache.synapse.transport.passthru.PassThroughHttpSSLSender} - HTTPS Sender shutdown {org.apache.synapse.transport.passthru.PassThroughHttpSSLSender}
TID: [0] [ESB] [2015-08-07 22:14:58,712] INFO {org.apache.synapse.transport.passthru.PassThroughHttpSender} - HTTP Sender shutdown {org.apache.synapse.transport.passthru.PassThroughHttpSender}
TID: [0] [ESB] [2015-08-07 22:14:58,712] INFO {org.apache.synapse.transport.passthru.PassThroughHttpSSLListener} - Destroying PassThroughHttpListener {org.apache.synapse.transport.passthru.PassThroughHttpSSLListener}
TID: [0] [ESB] [2015-08-07 22:14:58,713] INFO {org.apache.synapse.transport.passthru.PassThroughHttpListener} - Destroying PassThroughHttpListener {org.apache.synapse.transport.passthru.PassThroughHttpListener}
TID: [0] [ESB] [2015-08-07 22:14:58,960] INFO {org.wso2.carbon.tomcat.ext.internal.CarbonTomcatServiceComponent} - Stopping the carbon web-app registered under : / {org.wso2.carbon.tomcat.ext.internal.CarbonTomcatServiceComponent}
TID: [0] [ESB] [2015-08-07 22:15:00,251] INFO {org.wso2.carbon.core.init.CarbonServerManager} - Shutdown complete {org.wso2.carbon.core.init.CarbonServerManager}
TID: [0] [ESB] [2015-08-07 22:15:00,252] INFO {org.wso2.carbon.core.init.CarbonServerManager} - Halting JVM {org.wso2.carbon.core.init.CarbonServerManager}
然后你可以打电话:
> data
Gossypol Treatment Damage_cm
1 1036.3318 1c_2d 0.4955
2 4171.4277 3c_2d 1.5160
3 6039.9951 9c_2d 4.4090
4 5909.0682 1c_7d 3.2665
5 4140.2426 1c_2d 0.4910
...
54 2547.3262 1c_2d 0.5895
55 2608.7161 3c_2d 2.5590
56 1079.8465 C 0.0000
打印m<-nls(data$Gossypol~Y+A*(1-B^data$Damage_cm),data=data,start = list(Y=1000,A=3000,B=0.5))
为您提供:
m
现在您可以根据合适情况获取数据:
> m
Nonlinear regression model
model: data$Gossypol ~ Y + A * (1 - B^data$Damage_cm)
data: data
Y A B
1303.4450 2796.0385 0.4939
residual sum-of-squares: 1.03e+08
绘制数据以比较拟合和原始数据:
fitData <- 1303.4450 + 2796.0385*(1-0.4939^data$Damage_cm)
给你:
如果您想使用plot(data$Damage_cm, data$Gossypol, col='black')
par(new=T)
plot(data$Damage_cm,fitData, col='red', ylim=c(0,8000), axes=F, ylab='')
确保已安装,如果不是,则可以使用
nls2
这样做。
install.packages('nls2')
为您提供与library(nls2)
m2<-nls2(data$Gossypol~Y+A*(1-B^data$Damage_cm),data=data,start = list(Y=1000,A=3000,B=0.5))
相同的值:
nls
如果您更喜欢> m2
Nonlinear regression model
model: data$Gossypol ~ Y + A * (1 - B^data$Damage_cm)
data: structure(list(Gossypol = c(1036.331811, 4171.427741, 6039.995102, 5909.068158, 4140.242559, 4854.985845, 6982.035521, 6132.876396, 948.2418407, 3618.448997, 3130.376482, 5113.942098, 1180.171957, 1500.863038, 4576.787021, 5629.979049, 3378.151945, 3589.187889, 2508.417927, 1989.576826, 5972.926124, 2867.610671, 450.7205451, 1120.955, 3470.09352, 3575.043632, 2952.931863, 349.0864019, 1013.807628, 910.8879471, 3743.331903, 3350.203452, 592.3403778, 1517.045807, 1504.491931, 3736.144027, 2818.419785, 723.885643, 1782.864308, 1414.161257, 3723.629772, 3747.076592, 2005.919344, 4198.569251, 2228.522959, 3322.115942, 4274.324792, 720.9785449, 2874.651764, 2287.228752, 5654.858696, 1247.806111, 1247.806111, 2547.326207, 2608.716056, 1079.846532), Treatment = structure(c(2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 1L), .Label = c("C", "1c_2d", "3c_2d", "9c_2d", "1c_7d"), class = "factor"), Damage_cm = c(0.4955, 1.516, 4.409, 3.2665, 0.491, 2.3035, 3.51, 1.8115, 0, 0.4435, 1.573, 1.8595, 0, 0.142, 2.171, 4.023, 4.9835, 0, 0.6925, 1.989, 5.683, 3.547, 0, 0.756, 2.129, 9.437, 3.211, 0, 0.578, 2.966, 4.7245, 1.8185, 0, 1.0475, 1.62, 5.568, 9.7455, 0, 0.8295, 2.411, 7.272, 4.516, 0, 0.4035, 2.974, 8.043, 4.809, 0, 0.6965, 1.313, 5.681, 3.474, 0, 0.5895, 2.559, 0)), .Names = c("Gossypol", "Treatment", "Damage_cm"), row.names = c(NA, -56L), class = "data.frame")
Y A B
1303.4450 2796.0385 0.4939
residual sum-of-squares: 1.03e+08
Number of iterations to convergence: 2
Achieved convergence tolerance: 4.936e-06
:
ggplot2
虽然我担心标准错误必须在ggplot(data, aes(x = Damage_cm, y = Gossypol)) +
geom_point() +
geom_smooth(method = "nls",
formula = y ~ Y + A * (1 - B^x),
start = c(Y=1000, A=3000, B=0.5), se = F)
之外进行模拟。