我有数据,我正在努力使特定功能适合该数据,如下所示:
Mbin<-c(-26.74152,-26.32586,-26.16902,-26.03708,-25.86221,-25.70214,-25.55519,-25.39293,-25.23132,-25.07325,-24.92238,-24.76552,-24.60866,-24.44969,-24.29362,-24.13320,-23.97395,-23.81532,-23.66030,-23.49829,-23.34880,-23.17942,-23.02612,-22.86098,-22.71102,-22.54037,-22.38711,-22.22724,-22.07680,-21.90209,-21.77597,-21.63501,-21.44869,-21.28609,-21.12089,-20.95608,-20.79449,-20.66514,-20.44232,-20.08840,-19.79133,-19.73530,-19.41894,-18.82138)
Vmax<-c(6.367897e-08,8.167712e-07,2.659499e-06,9.590067e-06,2.360869e-05,5.405628e-05,1.156365e-04,2.285497e-04,4.290705e-04,7.599138e-04,1.142753e-03,1.693436e-03,2.085106e-03,2.539773e-03,2.790580e-03,3.174293e-03,3.199006e-03,3.094647e-03,3.139568e-03,2.997987e-03,2.890851e-03,2.543796e-03,2.622967e-03,2.455943e-03,2.023993e-03,1.787655e-03,2.041746e-03,2.600956e-03,2.315969e-03,1.599389e-03,1.029206e-03,6.840921e-04,7.460773e-04,3.788563e-04,1.887016e-03,1.778704e-03,7.452147e-04,1.292149e-03,2.377452e-03,2.812648e-03,2.972892e-03,1.640195e-03,2.534066e-03,5.970733e-03)
std<-c(4.061177e-07,4.254955e-06,9.480800e-06,1.872364e-05,3.938523e-05,6.586197e-05,1.172671e-04,1.983853e-04,2.943069e-04,3.844010e-04,4.348691e-04,4.405992e-04,4.003554e-04,3.516165e-04,2.723345e-04,2.188924e-04,1.654583e-04,1.253636e-04,1.561276e-04,2.493767e-04,2.485612e-04,3.070280e-04,2.915719e-04,3.183501e-04,3.554390e-04,3.884025e-04,3.664473e-04,3.959189e-04,3.779365e-04,5.254186e-04,6.164908e-04,6.461158e-04,5.528595e-04,5.441972e-04,6.005795e-04,5.316142e-04,6.339150e-04,6.033203e-04,8.283205e-04,1.014114e-03,1.485986e-03,1.005192e-03,1.732050e-03,3.132553e-03)
schechter<-function(phi,alpha,Mstar) { 0.4*(log(10))*phi*(10^(0.4*(Mstar-Mbin)*(alpha+1)))*exp(-10^(0.4*(Mstar-Mbin))) }
f = fitModel(Vmax~0.4*(log(10))*phi*(10^(0.4*(Mstar-Mbin)*(alpha+1)))*exp(-10^(0.4*(Mstar-Mbin))), data=data_to_fit, start=list(phi=0.024, alpha=-0.67,Mstar=-24.5), control=nls.control(200), trace=TRUE, weights=std)
plotPoints(log10(Vmax)~Mbin)
plotFun(log10(f(Mbin))~Mbin,add=TRUE)
当我从代码行中删除权重时,它可以工作。但那时合适并不好。这是我没有重量的合适:
我必须使用权重来获得更好的拟合,但是我得到了一个错误:
eval(expr,envir,enclos)中的错误: ..3在不正确的背景下使用,没有......查看
我还尝试使用nls()
和nlsLM()
来配合我的功能,但我无法适应我的功能。
这个问题有什么解决方案吗?
答案 0 :(得分:1)
我认为std
是标准偏差。这可能意味着您希望按1/std
甚至1/std^2
加权:
data_to_fit <- data.frame(Mbin, Vmax, std)
f = nls(Vmax~0.4*(log(10))*phi*(10^(0.4*(Mstar-Mbin)*(alpha+1)))*exp(-10^(0.4*(Mstar-Mbin))),
data=data_to_fit, start=list(phi=0.024, alpha=-0.67,Mstar=-24.5),
control=nls.control(200), trace=TRUE)
f_weighted = nls(Vmax~0.4*(log(10))*phi*(10^(0.4*(Mstar-Mbin)*(alpha+1)))*exp(-10^(0.4*(Mstar-Mbin))),
data=data_to_fit, start=as.list(coef(f)),
control=nls.control(2000, minFactor = 1e-10), trace=TRUE,
weights = 1/std)
f_weighted_sq = nls(Vmax~0.4*(log(10))*phi*(10^(0.4*(Mstar-Mbin)*(alpha+1)))*exp(-10^(0.4*(Mstar-Mbin))),
data=data_to_fit, start=as.list(coef(f)),
control=nls.control(2000, minFactor = 1e-10), trace=TRUE,
weights = 1/std^2)
plot(log10(Vmax)~Mbin)
curve(log10(predict(f, newdata = data.frame(Mbin = x))), from = -28, to = 22,
add = TRUE)
curve(log10(predict(f_weighted, newdata = data.frame(Mbin = x))), from = -28, to = 22,
add = TRUE, col = "dark green")
curve(log10(predict(f_weighted_sq, newdata = data.frame(Mbin = x))), from = -28, to = 22,
add = TRUE, col = "dark blue")