可能的包与 minpack.lm 冲突以进行 nlsLM 优化

Possible Package conflicts with minpack.lm for nlsLM optimization

问题:tidyverseggplot2dplyr是否可以与minpack.lm冲突 以某种方式导致 nlsLM 在下面给出此错误?

 "Error in numericDeriv(form[[3L]], names(ind), env) : 
  Missing value or an infinity produced when evaluating the model"

如果我运行脚本没有加载tidyverseggplot2dplyrnlsLM运行s 美好的。但是,加载这些包后,它会失败。我需要 tidyverseggplot2dplyr 用于其他目的。我已经为此工作了将近三个星期,但没有任何进展。有人知道解决这个问题的方法吗?

# Packages possibly conflicting with minpack.lm:

library(tidyverse)
library(ggplot2)
library(dplyr)

    df1<-structure(list(t = c(41.81456, 41.8979, 41.98122, 42.06455, 42.14788, 
42.23121, 42.31454, 42.39787, 42.48121, 42.56454, 42.64786, 42.73119, 
42.81451, 42.89785, 42.98118, 43.0645, 43.14782, 43.23115, 43.31448, 
43.39781, 43.48115, 43.56449, 43.64783, 43.73117, 43.81451, 43.89784, 
43.98117, 44.06451, 44.14784, 44.23118, 44.31451, 44.39785, 44.48119, 
44.56452, 44.64785, 44.73118, 44.81451, 44.89782, 44.98116, 45.06449, 
45.14782, 45.23115, 45.31448, 45.39782, 45.48115, 45.56447, 45.6478, 
45.73113, 45.81446, 45.89779, 45.98112, 46.06445, 46.14778, 46.23111, 
46.31444, 46.39777, 46.4811, 46.56443, 46.64776, 46.73109, 46.81443, 
46.89776, 46.98109, 47.06442, 47.14775, 47.23108, 47.31441, 47.39775, 
47.48107, 47.56441, 47.64776, 47.7311, 47.81444, 47.89777, 47.98109, 
48.06442, 48.14775, 48.23107, 48.3144, 48.39773, 48.48105, 48.56438, 
48.64771, 48.73105, 48.81437, 48.8977, 48.98103, 49.06436, 49.14769, 
49.23102, 49.31436, 49.39769, 49.48103, 49.56436, 49.64769, 49.73102, 
49.81436, 49.8977, 49.98104, 50.06438, 50.14771, 50.23105, 50.31439, 
50.39772, 50.48105, 50.56438, 50.64771, 50.73104, 50.81437, 50.89771, 
50.98103, 51.06437, 51.1477, 51.23102, 51.31436, 51.3977, 51.48104, 
51.56437, 51.64771, 51.73104, 51.81437, 51.8977), P = c(8.53088824439479, 
8.54319105274227, 8.5555134721853, 8.56660057261653, 8.56783757915745, 
8.57768800513352, 8.58814995423046, 8.59615183327934, 8.60475905763426, 
8.61151122664739, 8.61946930385678, 8.62562672212793, 8.63056169622599, 
8.63489484553338, 8.64039106397303, 8.64405303111868, 8.64957339880156, 
8.65326161930697, 8.65761233299738, 8.66131659363462, 8.66623040333336, 
8.66929200000484, 8.68039562456149, 8.68162507943826, 8.68287426533, 
8.69214593674048, 8.69343441809466, 8.70143769136384, 8.71004329016597, 
8.7192507719376, 8.72902653791303, 8.74682616577394, 8.75664164140497, 
8.77448123911484, 8.79174047830324, 8.81022129563025, 8.82812160684839, 
8.8441634691567, 8.8687915571925, 8.89340014894745, 8.92719379634901, 
8.95295878043183, 8.98491861330606, 9.01805456499124, 9.04270208267931, 
9.07593635545342, 9.10915089111537, 9.15035717467767, 9.18236246396164, 
9.22352781798659, 9.2542834391574, 9.29489432517773, 9.33429562925209, 
9.36386145814447, 9.40939043953831, 9.44635281486372, 9.4820463817321, 
9.53555998000974, 9.57742014994852, 9.62786613431272, 9.66968498605682, 
9.72009094496871, 9.76930733695951, 9.81848370500752, 9.86645026740704, 
9.91391414193938, 9.96131148668913, 10.0080360882848, 10.052913317416, 
10.0971191941511, 10.1555522193152, 10.21173263939, 10.259922523807, 
10.2982528980911, 10.3342257346323, 10.3528278963152, 10.3879101292573, 
10.4230772100412, 10.4680744971081, 10.5039735037242, 10.5379870034075, 
10.5789653267884, 10.6111749453033, 10.6508393242337, 10.6898506329465, 
10.7087817942742, 10.733861485256, 10.7502548560023, 10.7757396638767, 
10.7919670601155, 10.816147522643, 10.8402493791566, 10.8668049690756, 
10.8879433776052, 10.9097454472612, 10.9334224464139, 10.964974571933, 
10.9969326626233, 11.0308446892133, 11.0560694664594, 11.0800932317964, 
11.1040333226519, 11.1278941503041, 11.1516776611771, 11.1784807640745, 
11.2048059703341, 11.2241522962566, 11.2517081712771, 11.289149062585, 
11.3185710815297, 11.3380930113004, 11.3659382821831, 11.3938657029567, 
11.4121476074725, 11.439665508855, 11.4743283996282, 11.4988316648768, 
11.5243780650564, 11.5477464246768, 11.5709581673817, 11.5823109564472, 
11.6036048977594), C = c(0.650764810233295, 0.631656247736403, 
0.612574832717267, 0.592254327802933, 0.562083727098265, 0.540526543845557, 
0.519580881643709, 0.496175147233211, 0.473370986808824, 0.448715537753335, 
0.425269763502698, 0.400019558684505, 0.373550676082862, 0.346472427765102, 
0.320561014730082, 0.292819116425163, 0.266935615482259, 0.23921619498004, 
0.212159264203219, 0.184455876762495, 0.157958265545584, 0.129608437367395, 
0.109300632968539, 0.0791186546046685, 0.0489564027835421, 0.0268204008855406, 
-0.00329879593875582, -0.02670697500362, -0.0495090647673724, 
-0.0717130461716859, -0.0933449800756243, -0.106956827199008, 
-0.128552832845783, -0.142120953904213, -0.156269440336667, -0.169196355781731, 
-0.182703784798489, -0.198062132351044, -0.204845569545366, -0.211644742420704, 
-0.209258868499942, -0.214901667133789, -0.214349626614176, -0.212625246428741, 
-0.219385541649181, -0.217555323645168, -0.215748623270574, -0.205950186921044, 
-0.205352757293911, -0.195595275910987, -0.196247540931955, -0.187044555242156, 
-0.179051166251591, -0.180893267837376, -0.166772232984931, -0.161217820961941, 
-0.156932234884994, -0.134826635647281, -0.124374483789239, -0.105336537373178, 
-0.0949295134260186, -0.0759316348239327, -0.0581233457108326, 
-0.0403551040874213, -0.0237966926807225, -0.00774099477483325, 
0.00824814660338902, 0.0235607477828133, 0.0370334856643044, 
0.0498273024863298, 0.0768444668864738, 0.101612762271841, 0.118390487493773, 
0.125312435746874, 0.129880577876097, 0.117074236807964, 0.120747926128578, 
0.124510189835743, 0.138098846163867, 0.142589175633473, 0.145197718956352, 
0.154767266273982, 0.155568056034102, 0.16381978202047, 0.171425918885097, 
0.158948079322149, 0.152618707006763, 0.137602949353951, 0.131678560916043, 
0.116496689997248, 0.109264042187515, 0.101956480537829, 0.0970988025782429, 
0.0868276286254517, 0.0772200282912383, 0.0694872661722599, 0.0696257658632167, 
0.0701701313795393, 0.0726683291676462, 0.0664791695181872, 0.0590926546000645, 
0.0516185782156224, 0.044065115955533, 0.0364379783982276, 0.0318302994350521, 
0.0267445846615217, 0.0146798443901535, 0.0108245018115021, 0.0168540176031886, 
0.0148607267783909, 0.00297471408270056, -0.000595675807749885, 
-0.00408033311313183, -0.0172069319420931, -0.0211052764486634, 
-0.0178588436181535, -0.0247722574716391, -0.0306389973064007, 
-0.0386877879669711, -0.0468896766168481, -0.0669507804812142, 
-0.0770710048925398), v = c(2.73720919401795e-07, 2.89275866243833e-07, 
3.05704744509841e-07, 3.23056147643161e-07, 3.41383503107939e-07, 
3.60740020118146e-07, 3.81182894669021e-07, 4.02772468186816e-07, 
4.25573817135869e-07, 4.49652809926895e-07, 4.75077190571432e-07, 
5.01924516454949e-07, 5.30273542459307e-07, 5.60209325607651e-07, 
5.91819764376075e-07, 6.25191565024464e-07, 6.60423815060183e-07, 
6.97623624698831e-07, 7.36899824259678e-07, 7.78364716293996e-07, 
8.22141727880177e-07, 8.68358553356049e-07, 9.17146942649445e-07, 
9.68648499282585e-07, 1.02301254228321e-06, 1.08039295361324e-06, 
1.14095522628006e-06, 1.20488164276889e-06, 1.27235309266431e-06, 
1.34356428051965e-06, 1.41872017661923e-06, 1.49803720002012e-06, 
1.58174837208432e-06, 1.67008400106882e-06, 1.76329669689182e-06, 
1.86165861610844e-06, 1.96545125564181e-06, 2.07495771940026e-06, 
2.19051033032321e-06, 2.31244676896853e-06, 2.44109344748083e-06, 
2.57682364247604e-06, 2.72002332780748e-06, 2.87110867939643e-06, 
3.03049986417657e-06, 3.19862819087422e-06, 3.37598849722003e-06, 
3.56309366647097e-06, 3.76046212631028e-06, 3.96865098343192e-06, 
4.18824726536018e-06, 4.41986951018994e-06, 4.66416943984418e-06, 
4.92183372125163e-06, 5.1935858199306e-06, 5.48018795087751e-06, 
5.78244313168551e-06, 6.1011973433322e-06, 6.43734180406693e-06, 
6.7918153624452e-06, 7.16563024757515e-06, 7.55980712352996e-06, 
7.97541858133744e-06, 8.41364335579908e-06, 8.87569896478336e-06, 
9.36286778257749e-06, 9.87650045747503e-06, 1.04180531601306e-05, 
1.09889579629179e-05, 1.15908264520961e-05, 1.22254359968269e-05, 
1.28944314880812e-05, 1.35996142589735e-05, 1.43429180537019e-05, 
1.51263316467067e-05, 1.59520940241802e-05, 1.6822522665106e-05, 
1.77398981706101e-05, 1.87067840492661e-05, 1.97258854864512e-05, 
2.07998638330026e-05, 2.1931710964562e-05, 2.3124583860732e-05, 
2.43817436226076e-05, 2.57064566766011e-05, 2.71023147452351e-05, 
2.85732719977727e-05, 3.01232366788732e-05, 3.17564077655259e-05, 
3.34772050137959e-05, 3.52903930671811e-05, 3.72007656749664e-05, 
3.92134811622193e-05, 4.13339600995988e-05, 4.35677774133232e-05, 
4.59210633579047e-05, 4.84002939774841e-05, 5.10121441928917e-05, 
5.37634740647825e-05, 5.66616536161489e-05, 5.97142485508226e-05, 
6.29295921258743e-05, 6.63164729686435e-05, 6.98835178901844e-05, 
7.36402014328656e-05, 7.75967281240777e-05, 8.17636166483606e-05, 
8.61519334015006e-05, 9.07733206985704e-05, 9.56403286306054e-05, 
0.000100765247971781, 0.000106161936108279, 0.000111844981550703, 
0.000117828340858234, 0.00012412884956377, 0.000130763552542832, 
0.000137749179512345, 0.00014510363839137, 0.000152846660963375, 
0.000160998542157569, 0.000169580127136327, 0.000178614349528655
)), row.names = c(NA, -122L), class = c("tbl_df", "tbl", "data.frame"
))


    # minpack.lm for nlsLM
        install.packages("minpack.lm")
    library("minpack.lm")
    #objects & df used in ans.2 function:
    h<-552
    mp<-77
    R_<-0.00831
    K<-273.15
    gstart<-data.frame(h=c(550))#mp=c(70,80),h=c(500,600),mp=c(77)
    
# exponential function for finding v in df1:
ans.2<- function(x){
  h*((1/(1+exp((h/R_)*((1/(x+K))-(1/(mp+K))))))
     
     -(1/(1+exp((h/R_)*((1/(lag(x)+K))-(1/(mp+K))))))
     
     /((x-lag(x))+K))
}
ans.2(x=df1$t)# checking if function works!
# finding values of h and mp that will minimize the residual sum of squares(RSS) between C and v:
#non-expanded ans.2
nlsLM(formula= C~ans.2(x=df1$t),
      data=df1,
      start=list(h=550,mp=77),
      trace=T)
#nlsLM test:      
nlsLM(formula= C~h*((
  (1/(1+exp((h/R_)*((1/(t+K))-(1/(mp+K))))))
  
  -(1/(1+exp((h/R_)*((1/(lag(t)+K))-(1/(mp+K))))))
)

/((t-lag(t))+K)),
data=df1,
start=gstart, 
trace=T)

# saving nlsLM outputs
nls_1<- summary(nlsLM(formula= C~h*(((1/(1+exp((h/R_)*((1/(t+K))-(1/(mp+K))))))
                                     
                                     -(1/(1+exp((h/R_)*((1/(lag(t)+K))-(1/(mp+K))))))
                                     
                                     /((t-lag(t))+K))),
                      data=df1,
                      start=list(h=50),#,mp=77
                      trace=F))

这两个都是导致问题的两个屏蔽函数。但是,你一个电话还是电话tidyverse。 (当您调用 tidyverse 时,ggplot2dplyr 已被调用,因此无需再次调用它们。)要将这两个屏蔽函数改回原来的状态,请保持您的代码正常工作,同时使用 tidyverse,将其添加到代码中:filter <- stats::filterlag <- stats::lag,它将起作用。但是,如果您想使用这些调用的 dplyr 版本,您可以将它们改回或将库附加到函数。

library(tidyverse)
library(minpack.lm)

# change back the only two masked functions
filter <- stats::filter
lag <- stats::lag

从这里,调用您的数据对象 df1 和您最初调用的其他函数。

# objects & df used in ans.2 function:
h <- 552
mp <- 77
R_ <- 0.00831
K <- 273.15
gstart <- data.frame(h = c(550)) # mp=c(70,80), h=c(500,600), mp=c(77)
# exponential function for finding v in df1:
ans.2<- function(x){
  h * ((1/ (1 + exp((h/ R_)*((1/ (x + K)) - (1/(mp + K))))))
      -(1/ (1 + exp((h/ R_)*((1/ (lag(x) + K)) - (1/(mp + K))))))
     /((x - lag(x)) + K))
}
ans.2(x = df1$t) # checking if function works
# finding values of h and mp, minimize residual sum of squares (RSS) between C and v:
# non-expanded ans.2
nlsLM(formula = C~ ans.2(x = df1$t),
      data = df1,
      start = list(h = 550, 
                   mp = 77),
      trace = T)
#nlsLM test:      
nlsLM(formula = C ~ h*((
      (1/ (1 + exp((h / R_)*((1/(t + K))-(1/(mp + K))))))
      -(1/ (1 + exp((h / R_)*((1/(lag(t)+ K))-(1/(mp + K))))))
    ) /((t - lag(t)) + K)), data = df1, start = gstart, trace = T)
    
    # saving nlsLM outputs
nls_1 <- summary(nlsLM(formula= C~ h * (
  ((1/(1 + exp((h/ R_)*((1/ (t + K))-(1/ (mp + K))))))
   -(1/ (1 + exp((h/ R_) * ((1/ (lag(t) + K))-(1/ (mp + K))))))
   /((t - lag(t)) + K))),
  data = df1,
  start = list(h = 50), #,mp=77
  trace = F))

你会得到一个错误:

Error in nlsModel(formula, mf, start, wts) : 
  singular gradient matrix at initial parameter estimates

这是我找到的模型结果。

# Formula: C ~ h * (((1/(1 + exp((h/R_) * ((1/(t + K)) - (1/(mp + K)))))) - 
#     (1/(1 + exp((h/R_) * ((1/(lag(t) + K)) - (1/(mp + K))))))/((t - 
#         lag(t)) + K)))
# 
# Parameters:
#   Estimate Std. Error t value Pr(>|t|)    
# h   333.59      45.07   7.401 1.97e-11 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.2153 on 121 degrees of freedom
# 
# Number of iterations to convergence: 10 
# Achieved convergence tolerance: 1.49e-08
#