Very high residual Sum-of-Squares - r

I'm having a problem with the square sum-of-residues of an fitting. The square sum of the residues is too high which indicates that the fit is not very good. However, visually it looks fine to have this very high residual value ... Can anyone help me to know what's going on?
My data:
x=c(0.017359, 0.019206, 0.020619, 0.021022, 0.021793, 0.022366, 0.025691, 0.025780, 0.026355, 0.028858, 0.029766, 0.029967, 0.030241, 0.032216, 0.033657,
0.036250, 0.039145, 0.040682, 0.042334, 0.043747, 0.044165, 0.044630, 0.046045, 0.048138, 0.050813, 0.050955, 0.051910, 0.053042, 0.054853, 0.056886,
0.058651, 0.059472, 0.063770,0.064567, 0.067415, 0.067802, 0.068995, 0.070742,0.073486, 0.074085 ,0.074452, 0.075224, 0.075853, 0.076192, 0.077002,
0.078273, 0.079376, 0.083269, 0.085902, 0.087619, 0.089867, 0.092606, 0.095944, 0.096327, 0.097019, 0.098444, 0.098868, 0.098874, 0.102027, 0.103296,
0.107682, 0.108392, 0.108719, 0.109184, 0.109623, 0.118844, 0.124023, 0.124244, 0.129600, 0.130892, 0.136721, 0.137456, 0.147343, 0.149027, 0.152818,
0.155706,0.157650, 0.161060, 0.162594, 0.162950, 0.165031, 0.165408, 0.166680, 0.167727, 0.172882, 0.173264, 0.174552,0.176073, 0.185649, 0.194492,
0.196429, 0.200050, 0.208890, 0.209826, 0.213685, 0.219189, 0.221417, 0.222662, 0.230860, 0.234654, 0.235211, 0.241819, 0.247527, 0.251528, 0.253664,
0.256740, 0.261723, 0.274585, 0.278340, 0.281521, 0.282332, 0.286166, 0.288103, 0.292959, 0.295201, 0.309456, 0.312158, 0.314132, 0.319906, 0.319924,
0.322073, 0.325427, 0.328132, 0.333029, 0.334915, 0.342098, 0.345899, 0.345936, 0.350355, 0.355015, 0.355123, 0.356335, 0.364257, 0.371180, 0.375171,
0.377743, 0.383944, 0.388606, 0.390111, 0.395080, 0.398209, 0.409784, 0.410324, 0.424782 )
y= c(34843.40, 30362.66, 27991.80 ,28511.38, 28004.74, 27987.13, 22272.41, 23171.71, 23180.03, 20173.79, 19751.84, 20266.26, 20666.72, 18884.42, 17920.78, 15980.99, 14161.08, 13534.40, 12889.18, 12436.11,
12560.56, 12651.65, 12216.11, 11479.18, 10573.22, 10783.99, 10650.71, 10449.87, 10003.68, 9517.94, 9157.04, 9104.01, 8090.20, 8059.60, 7547.20, 7613.51, 7499.47, 7273.46, 6870.20, 6887.01,
6945.55, 6927.43, 6934.73, 6993.73, 6965.39, 6855.37, 6777.16, 6259.28, 5976.27, 5835.58, 5633.88, 5387.19, 5094.94, 5129.89, 5131.42, 5056.08, 5084.47, 5155.40, 4909.01, 4854.71,
4527.62, 4528.10, 4560.14, 4580.10, 4601.70, 3964.90, 3686.20, 3718.46, 3459.13, 3432.05, 3183.09, 3186.18, 2805.15, 2773.65, 2667.73, 2598.55, 2563.02, 2482.63, 2462.49, 2478.10,
2441.70, 2456.16, 2444.00, 2438.47, 2318.64, 2331.75, 2320.43, 2303.10, 2091.95, 1924.55, 1904.91, 1854.07, 1716.52, 1717.12, 1671.00, 1602.70, 1584.89, 1581.34, 1484.16, 1449.26,
1455.06, 1388.60, 1336.71, 1305.60, 1294.58, 1274.36, 1236.51, 1132.67, 1111.35, 1095.21, 1097.71, 1077.05, 1071.04, 1043.99, 1036.22, 950.26, 941.06, 936.37, 909.72, 916.45,
911.01, 898.94, 890.68, 870.99, 867.45, 837.39, 824.93, 830.61, 815.49, 799.77, 804.84, 804.88, 775.53, 751.95, 741.01, 735.86, 717.03, 704.57, 703.74, 690.63,
684.24, 650.30, 652.74, 612.95 )
Then make fit using the nlsLM function (minpack.lm package):
library(magicaxis)
library(minpack.lm)
sig.backg=3*10^(-3)
mod <- nlsLM(y ~ a *( 1 + (x/b)^2 )^c+sig.backg,
start = c(a = 0, b = 1, c = 0),
trace = TRUE)
## plot data
magplot(x, y, main = "data", log = "xy", pch=16)
## plot fitted values
lines(x, fitted(mod), col = 2, lwd = 4 )
This value is the residue:
> print(mod)
Nonlinear regression model
model: y ~ a * (1 + (x/b)^2)^c + sig.backg
data: parent.frame()
a b c
68504.2013 0.0122 -0.6324
residual sum-of-squares: 12641435
Number of iterations to convergence: 34
Achieved convergence tolerance: 0.0000000149
sum-of-squares residual is too high : 12641435 ...
Is that so or is something wrong with the adjustment? It is bad?

It makes sense, since the squared mean of your response variable is 38110960. You can scale your data if you prefer to work with smaller numbers.

The residual sum of squares doesn't have much meaning without knowing the total sum of squares (from which R^2 can be calculated). Its value is going to increase if your data have large values or if you add more data points, regardless of how good your fit is. Also, you may want to look at a plot of your residuals versus fitted data, there is a clear pattern that should be explained by your model to ensure that your errors are Normally distributed.

Related

MLE of a distribution in R: fitdistrplus (SGT distribution), how do I do it?

For return data I am doing research about the importance of skewness and kurtosis for the cVaR calculation. We are comparing some distributions first, by estimating the parameters of the distribution using fitdist() in R using package "fitdistrplus". However, we want to do this for a various number of distributions (see picture: SGT, GT, SGED, GED, t, norm).
Below is a sample code for SGT, where there is a problem: it introduced NaN's for the standard errors for parameters p and q. I also don't really know how to choose the starting values exactly.
SGTstart <- list(mu=0,sigma=2, lambda = 0.5, p=2, q=8)
SGTfit_R <- fitdistrplus::fitdist(data = as.vector(coredata(R)), distr = "sgt", method = "mle", SGTstart)
summary(SGTfit_R)
Sample of the data to make it reproducable: return vector of my stock index
c("0", "-1,008599424", "0,73180187", "0,443174024", "-0,351935172", "-1,318784086", "-2,171323799", "1,270243431", "-0,761354019", "0,417350946", "0,906432976", "-0,066736422", "-0,867085373", "-0,119914361", "-0,300989601", "0,482518259", "0,787365385", "-1,443105439", "-0,318546686", "-3,467674998", "1,041540157", "1,371281289", "-1,176752782", "-1,116893343", "-0,127522915", "-0,658070287", "1,098348016", "0,296391358", "-0,810635352", "-0,041779322", "0,353974233", "0,120090141", "0,304927119", "-1,22772592", "0,040768364", "1,182218724", "0,123136685", "-0,682709972", "-0,174093506", "-0,539704174", "0,579080595", "0,326346169", "0,205503526", "-0,771928642", "1,490828799", "0,734822712", "-0,025733101", "0,246531452", "-0,695585736", "-0,732413919", "0,806417952", "0,396105099", "0,024558388", "-0,791232528", "0,730410255", "-1,438890702", "0,668400286", "1,440996039", "0,731823553", "0,177515522", "0,740085418", "0,926248628", "-0,63516084", "-0,89996829", "1,655117371", "0,501033581", "0,06526534", "1,320866692", "-0,496350734", "-0,10157668", "0,022333393", "-1,236934596", "-1,070586427", "0,661662029", "0,871334714", "0,758891429", "0,064748766", "-0,305132153", "-0,424033661", "1,223444774", "-0,441840866", "-0,661390655", "-2,148399329", "0,843067435", "0,601099664", "-0,329590349", "0,210791225", "-0,341341769", "-0,555892395", "0,624026986", "0,218851965", "-0,015859171", "0,524283138", "-0,855634719", "0,339281481", "0,038507713", "-1,943784688", "0,315857689", "-0,368982834", "-1,111684011", "-0,2409217", "0,421815833", "-0,079319721", "0,915338199", "0,537387704", "-0,023004636", "-0,331854888", "0,702733882", "-1,084343115", "0,16901282", "0,559404916", "-0,538587484", "0,153683523", "-0,336562411", "-0,274946953", "0,862901957", "0,117407383", "1,205205829", "0,633347347", "0,058712615", "-0,083562948", "1,343190727", "1,281380185", "0,750972389", "-1,538678151", "0,228222073", "0,635385022", "0,037379479", "-0,491444798", "-1,220272752", "1,093162287", "1,499512169", "0,041394336", "-0,113330512", "0,657485999", "-0,264647978", "0,115056075", "-0,009763771", "0,454629881", "0,322398317", "0,347112494", "0,948127411", "0,461194301", "-0,407013048", "-0,469481931", "-0,536045151", "0,114726251", "0,396772868", "0,525885581")
Best, Enjo
The answer was using package sgt

Double summation in function which will be used for log likelihood estimation

I am trying to get the estimation parameters for the following equation:
First, I need to write the function with double summation. Then, I will use any optimization method to get the estimated parameters (can be nlm, optim etc). Here is my attempt (though, I am not sure how to add the double summation part in the end, two for loops):
There are 5 parameters: alpha_x, sigma_x, lambda_x, theta_x, phi_x >>> step_3[1] == alpha_x
for (a in 1:nrow(returns[[4]][,1])){
abc <- 0
for (b in 0:7){
abc <- abc + (step_3[3]^b*(factorial(b))^-1*(sqrt(step_3[2]^2+b*step_3[5]^2))^-1*exp(-1*(as.numeric(returns[[4]][a,1])-(step_3[1]+b*step_3[4]))^2*(2*(step_3[2]^2+b*step_3[5]^2))^-1))
}
abcd <- abcd + log(abc)
}
abcd <- -abcd + 0.5*nrow(returns[[4]][,1])*log(2*pi) + nrow(returns[[4]][,1])*step_3[3]
return(abcd)
}
nlm(s3, step_3 <- c(0.01,0.01,0.01,0.01,0.01), hessian = TRUE )
I am getting the estimation results, but the estimates are way off:
My question is how to implement (efficiently, any suggestions for sapply etc. would be appreciated) the double summation (sum_0_T) > ln (sum_1_l) into a function. The function will be used in optimization.
(l=0:7, T=number of observations)
return1 = some vector with price returns (all returns between -1,1).
After the estimation sample mean and variance should be roughly equal to:
sample_mean = alpha_x + lambda_x * theta_x
sample_variance = sigma_x^2 + lambda_x * (theta_x^2 + phi_x^2)
Thanks.
Here is a sample that can be used for the analysis:
c(0.02423747 ,-0.00419738, -0.03153278, 0.05343888, -0.0175492, 0.00848472, 0.01043673, -0.02123556, 0.01954968, -0.06462473, 0.02679873, 0.07971938, 0.00050474, -0.01768566,
-0.05206762, -0.00413691, 0.06390499, 0.00269576, 0.01520837, 0.00091051, 0.03499043, -0.00121999, -0.00123521, -0.01961684, 0.03360355, 0.01803711, 0.01772631, 0.036523
-0.00038927, 0.00905013, 0.01150976, 0.00480223, 0.01916402, 0.00054628, 0.01911904, 0.02194556, 0.00371314, 0.03376601, 0.0546574, -0.03972611, -0.0272525, 0.00271509,
0.02137819, 0.00483075, 0.03538795, 0.02981431, 0.00428509, -0.07192935, 0.01770175, -0.09626522, 0.07574215, 0.02929555, 0.01776551, 0.0385604, -0.06804089, 0.0666583,
0.01304272, -0.01825728, 0.01703525, 0.02022584, 0.03348027, 0.02818876, -0.00162942, -0.08785954, -0.13366772, 0.10243928)

How to calculate the root mean squared deviation in R?

I state that I never used the root mean squared deviation before.
I'm just tryng to reproduce what I found in an article.
Pratically, I have to quantify the noise of a "methodology" (that is the result of different noises, because of the coupling of two instruments), measuring the noise of the methodology at three different points out of the normal operation, where we know that it measures only the noise.
At the end, in the process that I'm following, you have to calculate the standard deviation among this three points and multiply it for the factor of 1.96 for the 95th percentile confidence interval (and in this way get a Limit of Detection of the method).
The time resolution is 30 minutes, then a standard deviation among three points, then the following three points, and so on. I already have a dataset organized in this way and I have calculated the standard deviation.
Just because I'm following the method of an article, they compare the limit of detection calculated using the standard deviation and the one using the root mean squared deviation.
Because at the end I have to use this limit of detection to filter my data by noise, I want to compare which is the best method for my situation, as they did.
How can I calculate the root mean squared deviation, as well as I calculated the standard deviation? Each three points (three differente columns), then the three following points (same three columns but the following row) and so on?
I already tried with the rmse function of the Metrics package, but the problem is the it require only two values: an actual and a predicted values.
Of course, as well as I did for the standard deviation calculation, I should use aggregate to iterate the function each three columns per each row.
EDIT
I put a piece of the article that I'm following for this method, to try to make you understand better what I would like
"For time lags much different from the true time lag, the standard deviation of the flux provides a measure of the random error affecting the flux"......."Multiplying this measure of the random error by α gives an estimate of the measurement precision at a given confidence interval (α =1.96 for the 95th percentile; α =3 for the 99th percentile) which can be used as the flux limit of detection (LoD) (i.e. LoDσ = α ×REσ )......"A modification of the LoDσ approach is to calculate the random error based on the root mean squared deviation (RMSE) of flux from zero ,which reflects the variability in the crosscovariance function in these regions, but also its offset from zero
I share with you part of the dataset and the code the I tried to use.
Dataset
structure(list(`101_LOD` = c(-0.00647656063436054, 0.00645714072316343,
0.00174533523902105, -0.000354643362187957, -0.000599093190801188,
0.00086188829059792), `101_LOD.1` = c(0.00380625456526623, -0.00398115037246045,
0.00158673927930099, -0.00537583996746438, -0.00280048350643599,
0.00348232298529063), `101_LOD.2` = c(-0.00281100080425964, -0.00335537844222041,
0.00611652518452308, -0.000738139825060029, 0.00485039477849737,
0.00412428118507656), `107_LOD` = c(0.00264717678436649, 0.00339296025595841,
0.00392733001719888, 0.0106686039973083, 0.00886643251752075,
0.0426091484273961), `107_LOD.1` = c(0.000242380702002215, -0.00116108069669281,
0.0119784744970561, 0.00380805756323248, 0.00190407945251567,
0.00199684331869391), `107_LOD.2` = c(-0.0102716279438754, -0.00706528150567528,
-0.0108745954674186, -0.0122962259781756, -0.00590383880635847,
-0.00166664119985051), `111_LOD` = c(-0.00174374098054644, 0.00383270191075735,
-0.00118363208946644, 0.00107908760333878, -9.30127551375776e-05,
-0.00141500588842743), `111_LOD.1` = c(0.000769378300959002,
0.00253820252869653, 0.00110643824418424, -0.000338050323261079,
-0.00313666295753596, 0.0043919374295125), `111_LOD.2` = c(0.000177265973907964,
0.00199829884609846, -0.000490950219515303, -0.00100263695578483,
0.00122606902671889, 0.00934018452187161), `113_LOD` = c(0.000997977666838309,
0.0062400770296875, -0.00153620247996209, 0.00136849054508488,
-0.00145700847633675, -0.000591288575933268), `113_LOD.1` = c(-0.00114161441697546,
0.00152607521404826, 0.000811193628975422, -0.000799514037634276,
-0.000319008435039752, -0.0010086036089075), `113_LOD.2` = c(-0.000722312098377764,
0.00364767954707251, 0.000547744649351312, 0.000352509651080838,
-0.000852173274761947, 0.00360487150682726), `135_LOD` = c(-0.00634051802134062,
0.00426062889500736, 0.00484049067127332, 0.00216220020394825,
0.00165634168942681, -0.00537970105199375), `135_LOD.1` = c(-0.00209301968088832,
0.00535855274344209, -0.00119679744329422, 0.0041216882161451,
0.00512978202611836, 0.0014048506490567), `135_LOD.2` = c(0.00022377545723911,
0.00400550696583795, 0.00198972253447825, 0.00301341644871015,
0.00256802839330668, 0.00946109288597202), `137_LOD` = c(-0.0108508893475138,
-0.0231919072487789, -0.00346546003410657, -0.00154066625155414,
0.0247266017774909, -0.0254464953061609), `137_LOD.1` = c(-0.00363025194918789,
-0.00291104074373261, 0.0024998477144967, 0.000877707284759669,
0.0095477003599792, 0.0501795740749602), `137_LOD.2` = c(0.00930498343499501,
-0.011839104725282, 0.000274929503053888, 0.000715665078729413,
0.0145503185102915, 0.0890428314632625), `149_LOD` = c(-0.000194406250680231,
0.000355157226357547, -0.000353931679163222, 0.000101471293242973,
-0.000429409422518444, 0.000344585379249552), `149_LOD.1` = c(-0.000494386150759807,
0.000384907974061922, 0.000582537329068263, -0.000173285705433721,
-6.92758935962043e-05, 0.00237942557324254), `149_LOD.2` = c(0.000368606958615297,
0.000432568466833549, 3.33092313366271e-05, 0.000715304544370804,
-0.000656902381786168, 0.000855422043674721), `155_LOD` = c(-0.000696168382693618,
-0.000917607266525328, 4.77049670728094e-06, 0.000140297660927979,
-5.99898679530658e-06, 6.71169142984434e-06), `155_LOD.1` = c(-0.000213644203677328,
-3.44396001911029e-07, -0.000524232671878577, -0.000830180665933627,
1.47799998238307e-06, -5.97640014667251e-05), `155_LOD.2` = c(-0.000749882784933487,
0.000345737159390042, -0.00076916001239521, -0.000135205762575321,
-2.55352420251723e-06, -3.07199008030628e-05), `31_LOD` = c(-0.00212014938530172,
0.0247411322547065, -0.00107990654365844, -0.000409195814154659,
-0.00768439381433953, 0.001860128524035), `31_LOD.1` = c(-0.00248488588195854,
-0.011146734518705, -0.000167943850441196, -0.0021998906531997,
0.0166775965182051, -0.0156939303287719), `31_LOD.2` = c(0.00210626277375321,
-0.00327815351414411, -0.00271043947479133, 0.00118991079627845,
-0.00838520090692615, 0.0255825346347586), `33_LOD` = c(0.0335175783154054,
0.0130192144768818, 0.0890608024914352, -0.0142431454793663,
0.00961009674973182, -0.0429774973256228), `33_LOD.1` = c(0.018600175159935,
0.04588362587764, 0.0517479021554752, 0.0453766081395813, -0.0483559729403664,
0.123771869764484), `33_LOD.2` = c(0.01906507758481, -0.00984821669825455,
0.134177176083007, -0.00544320457445977, 0.0516083894733814,
-0.0941500564321804), `39_LOD` = c(-0.148517395684098, -0.21311281527214,
0.112875846920874, -0.134256453140454, 0.0429030528286934, -0.0115143877745049
), `39_LOD.1` = c(-0.0431568202849291, -0.159003698955288, 0.0429009071238143,
-0.126060096927082, -0.078848020069061, -0.0788748111534866),
`39_LOD.2` = c(-0.16276833960171, 0.0236589399437796, 0.0828435027244962,
-0.50219849047847, -0.105196237549017, -0.161206838628339
), `42_LOD` = c(-0.00643926654994104, -0.0069253267922805,
7.63419856289838e-05, -0.0185223126108671, 0.00120855708103566,
-0.00275288147011515), `42_LOD.1` = c(-0.000866169150506504,
-0.00147791175852563, -0.000670310173141084, -0.00757733007180311,
0.0151353172950393, -0.00114193461500327), `42_LOD.2` = c(0.00719928454572906,
0.00311615354837406, 0.00270759483782046, -0.0108062423259522,
0.00158765505419478, -0.0034831499672973), `45_LOD` = c(0.00557787518897268,
0.022337270533665, 0.00657118689440082, -0.00247269227623608,
0.0191646343214611, 0.0233090596023039), `45_LOD.1` = c(-0.0305395220788143,
0.077105031761457, -0.00101713990356452, 0.0147500116150713,
-5.43009569586179e-05, -0.0235006181977403), `45_LOD.2` = c(-0.0216498682456909,
-0.0413426968184435, -0.0210779895848601, -0.0147549519865421,
0.00305229143870313, -0.0483293292336662), `47_LOD` = c(-0.00467568767221499,
-0.0199796182799552, 0.00985966068611855, -0.031010117051163,
0.0319279109813341, 0.0350743318265918), `47_LOD.1` = c(0.00820166533285921,
-0.00748186905620154, -0.010483251821707, -0.00921919551377505,
0.0129546148757833, 0.000223462281435923), `47_LOD.2` = c(0.00172469728530889,
0.0181683409295075, 0.00264937907258855, -0.0569837400476351,
0.00514558635349483, 0.0963339573489031), `59_LOD` = c(-0.00664210061621158,
-0.062069664217766, 0.0104345353700492, 0.0115323589989968,
-0.000701276829098035, -0.0397759501000331), `59_LOD.1` = c(-0.00844888486350536,
0.0207426674766074, -0.0227755432761471, -0.00370561240222376,
0.0152046240483297, -0.0127327412801225), `59_LOD.2` = c(-0.000546590647534814,
0.0178115310450356, 0.00776130696191998, 0.00162470375408126,
-0.036140754156005, 0.0197791914089296), `61_LOD` = c(0.00797528044191513,
-0.00358928087671818, 0.000662870138322471, -0.0412142836466128,
-0.00571822580078707, -0.0333870884803465), `61_LOD.1` = c(0.000105849888219735,
-0.00694734283847093, -0.00656216592134899, 0.00161225110022219,
0.0125744958934939, -0.0178560868664668), `61_LOD.2` = c(0.0049288443167774,
0.0059411543659837, -0.00165857112209555, -0.0093669075333705,
0.00655185371925189, 0.00516436591134869), `69_LOD` = c(0.0140014747729604,
0.0119645827116724, 0.0059880663080946, -0.00339119330845176,
0.00406436116298777, 0.00374425148741196), `69_LOD.1` = c(0.00465076983995792,
0.00664902297016735, -0.00183936649215524, 0.00496509351837152,
-0.0224812403463345, -0.0193087796456654), `69_LOD.2` = c(-0.00934638876711703,
-0.00802183076602164, 0.00406752039394799, -0.000421337136630527,
-0.00406768983408334, -0.0046016148041856), `71_LOD` = c(-0.00206064862123214,
0.0058604630066848, -0.00353440181333921, -0.000305197461077327,
0.00266085011303462, -0.00105635261106644), `71_LOD.1` = c(3.66652318354654e-06,
0.00542612739642576, 0.000860385212430484, 0.00157520645492044,
-0.00280256517377998, -0.00474358065422048), `71_LOD.2` = c(-0.00167098030843413,
0.0059622082597603, -0.00121597491543965, -0.000791592953383716,
-0.0022790991468459, 0.00508978650148816), `75_LOD` = c(NA,
-0.00562613898652477, -0.000103076958936504, -3.76628574664693e-05,
-0.000325767611573817, 0.000117404893823389), `75_LOD.1` = c(NA,
NA, -0.000496324358203359, -0.000517476831074487, -0.00213096062838051,
-0.00111202867609916), `75_LOD.2` = c(NA, NA, -0.000169651845347418,
-4.72864955070539e-05, -0.00144880109085214, 0.00421635976535877
), `79_LOD` = c(-0.0011901810540199, 0.00731686066269579,
0.00538551997145174, -0.00578723012473479, -0.0030246805255648,
0.00146141135533218), `79_LOD.1` = c(-0.00424278455960268,
-0.010593752642875, 0.0065136497427927, -0.00427355522802769,
0.000539975609490915, -0.0206849687839064), `79_LOD.2` = c(-0.00366739576561779,
-0.00374066839898667, -0.00132764684703939, -0.00534145222725701,
0.00920940542227595, -0.0101871763957068), `85_LOD` = c(-0.0120254177480422,
0.00369546541331518, -0.00420718877886963, 0.00414911885475517,
-0.00130381692844529, -0.00812757789798261), `85_LOD.1` = c(-0.00302024868281014,
0.00537704163310547, 0.00184264538884543, -0.00159032685888543,
-0.0062127769817834, 0.00349476605688194), `85_LOD.2` = c(0.0122689407380797,
-0.00509605601025503, -0.00641413996554198, 0.000592176121486696,
0.00131237912317341, -0.00535018996837309), `87_LOD` = c(0.00613621268007298,
0.000410268892659307, -0.00239014321624482, -0.00171179729894864,
-0.00107159765522861, -0.00708388174601732), `87_LOD.1` = c(0.00144787264098156,
-0.0025946273860992, -0.00194897899110034, 0.00157863310440493,
-0.0048913305554607, -0.000585669821053749), `87_LOD.2` = c(-0.00224691693198253,
-0.00277315666829267, 0.00166487067514155, -0.00173757960229744,
-0.00362252480121682, -0.0101992979591839), `93_LOD` = c(-0.0234225447373586,
0.0390095666365413, 0.00606244490932179, 0.0264258422783391,
0.0161211132913951, -0.0617678157059), `93_LOD.1` = c(-0.0124876313221369,
-0.0309636779639578, 0.00610883313140442, -0.0192442672220773,
0.0129557286224975, -0.00869066964782635), `93_LOD.2` = c(-0.0219837540560547,
-0.00521242297372905, 0.0179965615561871, 0.0081370991723329,
1.45427765512579e-06, -0.0111199632179688), `99_LOD` = c(0.00412086456443205,
-0.00259940538393106, 0.00742537463584133, -0.00302091572866969,
-0.00320466045653491, -0.00168702410433936), `99_LOD.1` = c(0.00280546156134205,
-0.00472591065687533, 0.00518402193979284, -0.00130887074314965,
0.00148769905391341, 0.00366250488078969), `99_LOD.2` = c(-0.00240469207099292,
-9.57307699040024e-05, -0.000145493235845501, 0.000667454164326723,
-0.0057445759245933, 0.00433464631989088), H_LOD = c(-6248.9128518109,
-10081.9540490064, -6696.91582671427, -5414.20614601348,
-3933.64339240365, -13153.7509294302), H_LOD.1 = c(-6.2489128518109,
-10.0819540490064, -6.69691582671427, -5.41420614601348,
-3.93364339240365, -13.1537509294302), H_LOD.2 = c(-6248.9128518109,
-10081.9540490064, -6696.91582671427, -5414.20614601348,
-3933.64339240365, -13153.7509294302)), row.names = c(NA,
6L), class = "data.frame")
Code
LOD_rdu=sapply(split.default(LOD_ut, rep(seq((ncol(LOD_ut) / 3)), each = 3)), function(i)
apply(i, 1, rmse))
And I get this error Error in mse(actual, predicted) :
argument "predicted" is missing, with no default
It is difficult to understand precisely what you need, I will try to answer you,
From wikipedia, RMSD serves to compare a dataset generated by a model (the model in your article i guess) to an observed distribution.
From CRAN, RMSE function in modelr package has two arguments, model and data:
modelr::rmse(model = ,data = )
This function will give you the fit of your model to the data. The first argument is a model, meaning you will probably use a function like lm() to generate it. Because you don't detail the model I can't help you more.
The second argument is the dataset, the one you provide is quite disturbing to me. R will expect a tidy set with two columns x the time of the observation and y the value.
you can first what groups your columns:
GRP = sub("[.][0-9]*","",colnames(LOD_ut))
head(GRP)
[1] "101_LOD" "101_LOD" "101_LOD" "107_LOD" "107_LOD" "107_LOD"
you can also use 1:(ncol(LOD_ut)/3), just that the above gives you the groups back.
We can use the above like this:
LOD_ut[,GRP=="101_LOD"]
101_LOD 101_LOD.1 101_LOD.2
1 -0.0064765606 0.003806255 -0.0028110008
2 0.0064571407 -0.003981150 -0.0033553784
3 0.0017453352 0.001586739 0.0061165252
4 -0.0003546434 -0.005375840 -0.0007381398
5 -0.0005990932 -0.002800484 0.0048503948
6 0.0008618883 0.003482323 0.0041242812
This calls out your first three grouped columns. Now if you do apply(..,1,sd) you get the standard deviation, now we just do it over all groups
SD = sapply(unique(GRP),function(i)apply(LOD_ut[,GRP==i],1,sd,na.rm=T))
If you must do RMSE, use predicted to be the mean:
rmse_func = function(x){
Metrics:::rmse(mean(x,na.rm=T),x[!is.na(x)])
}
RMSE = sapply(unique(GRP),function(i){
apply(LOD_ut[,GRP==i],1,rmse_func)
})

Search for optimal parameter value with curve fitting (difference equations)

I have two experimentally determined variables (y1_exp, y2_exp) which change with x, where y2_exp affects y1_exp with changing x (however y1_exp does not affect y2_exp). Here is the data:
library(tidyverse)
library(minpack.lm)
data <- read_csv("x,y1_exp,y2_exp
0,0.00553214,0.006103046
5,0.005526549,0.006079372
10,0.005521163,0.006092119
15,0.005528608,0.006065714
20,0.005526002,0.006062165
25,0.005523214,0.006057619
30,0.005523214,0.006048626
35,0.005523214,0.006044087
40,0.005523214,0.006044087
45,0.005520607,0.006048626
50,0.005512257,0.006025025
55,0.005520607,0.006013361
60,0.005518752,0.006007923
65,0.005524318,0.006025025
70,0.005520607,0.006016856
75,0.005513185,0.006002485
80,0.005515968,0.005995235
85,0.005788141,0.006016856
90,0.005733146,0.006033983
95,0.005683015,0.006028386
100,0.005644958,0.006013984
105,0.005618042,0.006004894
110,0.005604063,0.006010348
115,0.005590122,0.006010348
120,0.005572465,0.005997623
125,0.005572465,0.006000349
130,0.005567818,0.00599944
135,0.005567818,0.006000349
140,0.005569677,0.005997623
145,0.005567818,0.005990351
150,0.005567818,0.00598308
155,0.00555401,0.005977626
160,0.005569677,0.00598308
165,0.005569677,0.005977626
170,0.00556503,0.00598287
175,0.005562242,0.00598287
180,0.005572465,0.005974678
185,0.005578041,0.00598287
190,0.005586308,0.005971947
195,0.005585377,0.005966486
200,0.005588169,0.00598287
205,0.005602975,0.005978088
210,0.005628144,0.005991762
215,0.005642127,0.005992449
220,0.005644924,0.005986971
225,0.00565609,0.005984232
230,0.00565609,0.005994952
235,0.005667294,0.005997695
240,0.005672896,0.006000438
245,0.005684107,0.006001111
250,0.005696264,0.006009352
255,0.005707486,0.005998364
260,0.005710292,0.006012776
265,0.005724372,0.006003606
270,0.005732801,0.006018033
275,0.005758179,0.006018033
280,0.005769437,0.006018033
285,0.005780694,0.006026969
290,0.005791952,0.006028809
295,0.00581447,0.006026969
300,0.005828544,0.006035918
305,0.005845434,0.006037761
310,0.005856694,0.006037761
315,0.005870771,0.006042366
320,0.005886987,0.006040524
325,0.005892626,0.006040524
330,0.005909544,0.006054105
335,0.005922704,0.006054105
340,0.005922704,0.00605964
345,0.005934302,0.006062408
350,0.00593995,0.006076028
355,0.005942775,0.006076028
360,0.005960063,0.006090599
365,0.005982692,0.006087822
370,0.005998722,0.006090599
375,0.006004379,0.006095226
380,0.005998722,0.006107052
385,0.006010037,0.006104272
390,0.006012866,0.006104272
395,0.006018524,0.006112613
400,0.006041606,0.00612354
405,0.006047273,0.006117247
410,0.006047273,0.006117247
415,0.006039717,0.006122808
420,0.006048663,0.006131894
425,0.006041606,0.006134679
430,0.006058607,0.006137464
435,0.006067108,0.00613115
440,0.006067108,0.006140249
445,0.006064274,0.006136711
450,0.006061441,0.00613115
455,0.006076094,0.006139492
460,0.00605623,0.006147834
465,0.006067581,0.006159743
470,0.006078932,0.006159743
475,0.006085093,0.006164385
480,0.006104989,0.00617911
485,0.006139098,0.00617911
490,0.00614194,0.0061819
495,0.006147049,0.006184689
500,0.00614194,0.006186548
505,0.006127728,0.006190268
510,0.006147241,0.006203177
515,0.006158628,0.006208764
520,0.006163373,0.006211558
525,0.006171914,0.006220773
530,0.006171914,0.006220773
535,0.006188997,0.006223571
540,0.006194692,0.006226369
545,0.006201014,0.006231965
550,0.006206717,0.006244011
555,0.006206717,0.00623756
560,0.006205767,0.006249616
565,0.006203533,0.006249616
570,0.006189255,0.00625522
575,0.00619211,0.006264498
580,0.006177832,0.006265434
585,0.00617554,0.00626824
590,0.00617268,0.006271047
595,0.00617554,0.006271047
600,0.006186979,0.00627666
605,0.006189838,0.006280347
610,0.00617554,0.006285969
615,0.006181259,0.006285969
620,0.00617268,0.006283158
625,0.006173226,0.006283158
630,0.00621487,0.006283158
635,0.00662509,0.006246725
640,0.006882793,0.006060563
645,0.007245471,0.005928867
650,0.008473559,0.006350231
655,0.009247253,0.006737696
660,0.009883996,0.007087109
665,0.010312034,0.007251846
670,0.010135539,0.00731662
675,0.009749611,0.007002351
680,0.009783859,0.006887831
685,0.010029351,0.006935397
690,0.01016614,0.007080142
695,0.010375834,0.007156219
700,0.010447978,0.007188014
705,0.010649555,0.007368508
710,0.010884113,0.007595682
715,0.010975526,0.007633395
720,0.011137886,0.007690818
725,0.011303968,0.007845451
730,0.011504503,0.008027457
735,0.011601149,0.008080757
740,0.011728738,0.008191208
745,0.011446146,0.008243414
750,0.010941854,0.008023407
755,0.010871756,0.007969683
760,0.010906964,0.00798611
765,0.010976065,0.008050875
770,0.011007462,0.008234035
775,0.011119685,0.008294425
780,0.011392084,0.008469461
785,0.011416786,0.008598086
790,0.011426212,0.008616628
795,0.011443019,0.008600298
800,0.011487009,0.008696896
805,0.011484189,0.008789979
810,0.011547174,0.008854289
815,0.011329945,0.008795137
820,0.01118489,0.008819941
825,0.01122512,0.008823249
830,0.011235391,0.008928913
835,0.011128169,0.00898777
840,0.011073376,0.008993363
845,0.010992715,0.009012943
850,0.01111164,0.009076926
855,0.011218571,0.009180769
860,0.01169673,0.009473429
865,0.011927231,0.009710296
870,0.012101153,0.009861681
875,0.011931468,0.009703051
880,0.01179021,0.009621127
885,0.011744728,0.009595428
890,0.011687705,0.009624336
895,0.011663317,0.009649306
900,0.011775132,0.009686622
905,0.011606612,0.009745953
910,0.011546408,0.009753433
915,0.011493062,0.009792618
920,0.01154355,0.009824813
925,0.01148163,0.009869676
930,0.011417726,0.009874449
935,0.0113977,0.009890256
940,0.011315945,0.009891715
945,0.011219218,0.009903602
950,0.011128907,0.009908787
955,0.011021509,0.009873616
960,0.010931127,0.009864977
965,0.010870579,0.009871005
970,0.011099307,0.009939085
975,0.011633858,0.010239904
980,0.012124254,0.010485295
985,0.012055284,0.010508016
990,0.011873916,0.010467112
995,0.011669026,0.010341881
1000,0.01151546,0.01029192
1005,0.011362137,0.010227859
1010,0.011214379,0.010158445
1015,0.01141196,0.010189825
1020,0.011609125,0.010348048
1025,0.011478788,0.010374885
1030,0.011285931,0.010258824
1035,0.011100027,0.010180051
1040,0.010949988,0.010139262
1045,0.010811721,0.010089286
1050,0.011463506,0.010302578
1055,0.011543184,0.010475958
1060,0.011519785,0.010444628
1065,0.011597506,0.010448
1070,0.011625158,0.010433885
1075,0.011673233,0.010499426
1080,0.011623437,0.010488115
1085,0.011556184,0.010473977
1090,0.011455804,0.010461175
1095,0.011468944,0.010469645
1100,0.011446254,0.010480337
1105,0.01139332,0.010475958
1110,0.011295969,0.010475958
1115,0.011206453,0.010441904
1120,0.011113431,0.010435657
1125,0.011085183,0.010434068
1130,0.010985393,0.010430596
1135,0.011187642,0.010504032
1140,0.011678679,0.010704961
1145,0.011937,0.010935956
1150,0.012063323,0.011025534
1155,0.012075523,0.011070395
1160,0.011995448,0.011079496
1165,0.011926247,0.011053494
1170,0.011872694,0.011043828
1175,0.011765039,0.011006141
1180,0.011667538,0.010975287
1185,0.011552344,0.010921466
1190,0.011420588,0.010858289
1195,0.011286696,0.010794546
1200,0.011520687,0.010848888
1205,0.011868758,0.011034132
1210,0.011766439,0.011116272
1215,0.011543698,0.011015375
1220,0.011364646,0.010913784
1225,0.011217294,0.010832708
1230,0.011090832,0.010772
1235,0.010976536,0.010722906
1240,0.010882105,0.010685284
1245,0.010790014,0.010637493
1250,0.010675908,0.01059083
1255,0.010611841,0.010551785
1260,0.010520118,0.01049439
1265,0.010436363,0.010462116
1270,0.010365772,0.010416168
1275,0.010283637,0.010364018
1280,0.010210809,0.010336655
1285,0.010150285,0.010270346
1290,0.0100988,0.010250441
1295,0.010036937,0.010209833
1300,0.009969535,0.010162162
1305,0.0098893,0.010102105
1310,0.009811587,0.010069935
1315,0.00975446,0.010028163
1320,0.009710339,0.009973177
1325,0.009659878,0.009910431
1330,0.009597342,0.009903557
1335,0.009534978,0.009862383
1340,0.009441767,0.009811664
1345,0.009402898,0.009765549
1350,0.009555879,0.0097702
1355,0.009819939,0.009819344
1360,0.009979651,0.009902473
1365,0.010059698,0.00993667
1370,0.010138605,0.009974397
1375,0.010146579,0.009986544
1380,0.010124895,0.009972081
1385,0.010105922,0.009951555
1390,0.010063733,0.009927461
1395,0.010055871,0.009923
1400,0.010130486,0.009923
1405,0.01025683,0.009958444
1410,0.010352893,0.009993078
1415,0.010406697,0.010036551
1420,0.010394966,0.010040813
1425,0.01037857,0.010059473
1430,0.010364907,0.010071851
1435,0.010320278,0.010053811
1440,0.010245734,0.010034871
1445,0.010159804,0.010004539
1450,0.010079454,0.009978771
1455,0.009973549,0.009944079
1460,0.009914505,0.009903577
1465,0.009818433,0.009862329
1470,0.009757147,0.009836862
1475,0.009727332,0.009806513
1480,0.0098109,0.009814869
1485,0.009880298,0.009838812
1490,0.009905426,0.009868682
1495,0.009898913,0.009880827
1500,0.009881097,0.009897439
1505,0.009843015,0.009896232
1510,0.010190072,0.009962645
1515,0.010599498,0.010127028
1520,0.010942795,0.010263008
1525,0.011170186,0.010381296
1530,0.011395782,0.010486056
1535,0.011548349,0.010574059
1540,0.01176725,0.010667444
1545,0.011854934,0.010749314
1550,0.011749957,0.010777671
1555,0.011645574,0.010769029
1560,0.01153174,0.010751923
1565,0.01142172,0.010713116
1570,0.01132999,0.010701874
1575,0.011643676,0.010777677
1580,0.012083217,0.010966761
1585,0.012234384,0.011089843
1590,0.01230373,0.011191853
1595,0.012350221,0.011229474
1600,0.012288471,0.011247202
1605,0.01223059,0.011238763
1610,0.012172839,0.011201678
1615,0.012078904,0.01119393
1620,0.011996912,0.011158662
1625,0.011895883,0.011159118
1630,0.011740926,0.011088439
1635,0.011676492,0.011087635
1640,0.011528284,0.011017164
1645,0.011790256,0.011085255
1650,0.011953398,0.011129903
1655,0.012046214,0.011223616
1660,0.012033716,0.011271508
1665,0.011999419,0.011260053
1670,0.011980367,0.011262917
1675,0.011934646,0.011261007
1680,0.011831799,0.011251461
1685,0.011778945,0.011223616
1690,0.011643044,0.011198312
1695,0.011550501,0.011172109
1700,0.011461248,0.011135939
1705,0.011361141,0.011090447
1710,0.011220276,0.011014545
1715,0.011088828,0.0109503
1720,0.010983128,0.010912937
1725,0.010792283,0.010821904
1730,0.010785869,0.0107725
1735,0.010750802,0.010723305
1740,0.010724394,0.010727408
1745,0.011020584,0.01077812
1750,0.01146445,0.011004902
1755,0.011635392,0.011139519
1760,0.011612069,0.011182085
1765,0.011540083,0.011148072
1770,0.011446144,0.011115929
1775,0.011369663,0.011066511
1780,0.011327267,0.011051513
1785,0.011728344,0.01111432
1790,0.011871734,0.011236811
1795,0.011881626,0.01120834
1800,0.011876553,0.011194875
1805,0.011841874,0.01116778
1810,0.011800381,0.01116778
1815,0.011834656,0.011162121
1820,0.011859992,0.01115675
1825,0.011904479,0.01120268
1830,0.011887852,0.01121683
1835,0.011930542,0.01120834
1840,0.011897012,0.01121683
1845,0.011772847,0.011230774
1850,0.011648206,0.01120268
1855,0.011587653,0.011161459
1860,0.011609475,0.011155129
1865,0.011606612,0.011167355
1870,0.01163715,0.011165474
1875,0.011702345,0.011181461
1880,0.011630658,0.011184283
1885,0.011704526,0.011198191
1890,0.011687294,0.011195365
1895,0.011753668,0.011204784
1900,0.011730171,0.01128841
1905,0.01169363,0.011272173
1910,0.011702284,0.011255141
1915,0.011734749,0.011251356
1920,0.011730171,0.011282397
1925,0.011712211,0.011309709
1930,0.011755791,0.011301904
1935,0.011715105,0.011313311
1940,0.011647555,0.011296201
1945,0.011616716,0.01129335
1950,0.011570587,0.011280993
1955,0.011526489,0.011261305
1960,0.011478671,0.011255611
1965,0.011398653,0.011232835
1970,0.011357835,0.011207538
1975,0.011266999,0.01117283
1980,0.011228383,0.011140089
1985,0.011168425,0.011120624
1990,0.011365904,0.011122848
1995,0.011927272,0.01128274
2000,0.012269557,0.011463536
2005,0.012610168,0.011623446
2010,0.012178617,0.011607966
2015,0.012009799,0.011539489
2020,0.011862455,0.011453127
2025,0.011738504,0.011402674
2030,0.011623752,0.011331657
2035,0.011528515,0.011290242
2040,0.011422442,0.011249917
2045,0.011297187,0.011192985
2050,0.011224108,0.011134442
2055,0.01113363,0.01109932
2060,0.011045227,0.011065237
2065,0.011351941,0.011120562
2070,0.011576883,0.01120359
2075,0.011567037,0.011223171
2080,0.011478605,0.011215587
2085,0.011391364,0.011166053
2090,0.011298781,0.01111384
2095,0.011181323,0.011080428
2100,0.011093248,0.011002934
2105,0.010982501,0.010963566
2110,0.010883381,0.01090522
2115,0.010814714,0.010859742
2120,0.010721912,0.010796415
2125,0.010683917,0.010766847
2130,0.010580852,0.010725531
2135,0.010116913,0.010564735
2140,0.009553532,0.010407921
2145,0.009338262,0.010417143
2150,0.009155087,0.010448563
2155,0.008953537,0.010404576
2160,0.008783894,0.010359066
2165,0.008633234,0.010342206
2170,0.008503224,0.010270291
2175,0.008354554,0.010227032
2180,0.00827382,0.010135737
2185,0.008141687,0.010084595
2190,0.008032464,0.010089726
2195,0.007964806,0.010009439
2200,0.007898842,0.009945284
2205,0.008107711,0.009910075
2210,0.007992595,0.009844644
2215,0.007960512,0.009825585
2220,0.007998377,0.009843155
2225,0.008159075,0.009845001
2230,0.007983655,0.009766526
2235,0.00784587,0.009570522
2240,0.007507169,0.009276039
2245,0.007153126,0.009055356
2250,0.006969728,0.008921778
2255,0.006750942,0.008829258
2260,0.006683758,0.008575119
2265,0.006760147,0.008532829
2270,0.006586153,0.008526863
2275,0.006708381,0.008433861
2280,0.006529335,0.008342521
2285,0.00642451,0.008397442
2290,0.006863021,0.008618997
2295,0.006855758,0.008662227
2300,0.006786828,0.008711552
2305,0.006929332,0.008715441
2310,0.006950265,0.008713845
2315,0.006885391,0.008718283
2320,0.006809618,0.008671087
2325,0.007080174,0.008741003
2330,0.007041501,0.008670396
2335,0.006937773,0.008681892
2340,0.006842371,0.00864445
2345,0.007110533,0.008602165
2350,0.006981684,0.008549515
2355,0.006870447,0.008522203
2360,0.007093723,0.00856686
2365,0.006991019,0.008483845
2370,0.006858281,0.008487377
2375,0.007004398,0.008393025
2380,0.006777853,0.008164761
2385,0.006505572,0.007962258
2390,0.006511813,0.007893978
2395,0.006374155,0.007896467
2400,0.006662963,0.007973094
2405,0.006567388,0.008032516
2410,0.006718302,0.008016534
2415,0.006707408,0.008036541
2420,0.006590093,0.008069528
2425,0.006832484,0.008044899
2430,0.006708111,0.008031266
2435,0.006611892,0.008018998
2440,0.006838235,0.008003074
2445,0.006704078,0.007986184
2450,0.006720489,0.008019776
2455,0.006792322,0.007947677
2460,0.006655819,0.007975837
2465,0.006824524,0.007934253
2470,0.006750867,0.007881679
2475,0.006636794,0.007900702
2480,0.006825441,0.007883455
2485,0.006679557,0.007879763
2490,0.006826358,0.007877862
2495,0.006758137,0.007808681
2500,0.006748794,0.00787422
2505,0.006825446,0.007796849
2510,0.006683937,0.007843116
2515,0.006848461,0.007754702
2520,0.006694326,0.007813946
2525,0.006884377,0.007754702
2530,0.006710943,0.007776575
2535,0.006896459,0.007763526
2540,0.006738777,0.007731582
2545,0.006737715,0.007694166
2550,0.006769278,0.007683216
2555,0.006593724,0.007674512
2560,0.006824524,0.007680479
2565,0.006655819,0.007632121
2570,0.006802394,0.007624822
2575,0.00670024,0.007622085
2580,0.006649097,0.007594717
2585,0.00672338,0.007587419
2590,0.006545242,0.007602927
2595,0.006748197,0.007557317
2600,0.00658554,0.007505998
2605,0.00677122,0.007597453
2610,0.006589524,0.007501445
2615,0.006753831,0.007505998
2620,0.006610886,0.007479591
2625,0.006735563,0.007493008
2630,0.006612046,0.007471186
2635,0.006672271,0.007464822
2640,0.006638575,0.007433002
2645,0.006616383,0.00740573
2650,0.006638023,0.007382001
2655,0.006551678,0.007362941
2660,0.006662652,0.007389262
2665,0.006506334,0.007340253
2670,0.006646377,0.007321173
2675,0.006493024,0.007338389
2680,0.006673791,0.007323891
2685,0.006502498,0.007306655
2690,0.006682945,0.007293989
2695,0.006525259,0.007284942
2700,0.006685669,0.007274087
2705,0.006504655,0.007274087
2710,0.006666662,0.007239773
2715,0.006492267,0.007250612
2720,0.006675731,0.007263257
2725,0.006494083,0.007228935
2730,0.006674824,0.007228994
2735,0.006500747,0.007202841
2740,0.00669387,0.007210056
2745,0.006525221,0.007160461
2750,0.006675768,0.00717579
2755,0.006520048,0.007168576
2760,0.006678484,0.007168576
2765,0.006530909,0.007160461
2770,0.006665841,0.007155052
2775,0.006518489,0.007144232
2780,0.006665841,0.007136118
2785,0.00650674,0.007107489
2790,0.006645094,0.007124593
2795,0.00647541,0.007098488
2800,0.006618141,0.007079829
2805,0.006423782,0.007053133
2810,0.006581413,0.007050441
2815,0.006414786,0.007037252
2820,0.006592181,0.007041732
2825,0.006425581,0.007030084
2830,0.006597565,0.007034564
2835,0.006422882,0.007020229
2840,0.006592181,0.007020229
2845,0.006393199,0.006994247
2850,0.006545637,0.006973096
2855,0.006386386,0.006975779
2860,0.006564452,0.006975779
2865,0.006395719,0.006981146
2870,0.006551013,0.006963256
2875,0.006397163,0.006944473
2880,0.006542949,0.006963256
2885,0.006417243,0.00694179
2890,0.006514557,0.006926092
2895,0.006457966,0.006978463
2900,0.006682091,0.007014853
2905,0.006552118,0.00700109
2910,0.006646853,0.006994989
2915,0.006498219,0.006966195
2920,0.006601762,0.006965296
2925,0.006512636,0.006952699
2930,0.006603662,0.006952699
2935,0.006500251,0.006948201
2940,0.006593839,0.006921687
2945,0.006481357,0.006929772
2950,0.006628942,0.006913603
2955,0.00650565,0.006934705
2960,0.006644226,0.00692391
2965,0.006534263,0.006930207
2970,0.006581761,0.006930627
2975,0.006547781,0.006917112
2980,0.006563851,0.006911707
2985,0.006491254,0.006883428
2990,0.006572239,0.006867237
2995,0.006467225,0.006865997
3000,0.006593916,0.006865997
3005,0.006437582,0.006874081
3010,0.006567953,0.006876775
3015,0.006378303,0.006848601
3020,0.006519657,0.00683784
3025,0.006446841,0.006814229
3030,0.006451147,0.006794532
3035,0.006496538,0.006797824
3040,0.006402035,0.006795142
3045,0.006494157,0.006798413
3050,0.006330998,0.006779667
3055,0.006438382,0.006760657
3060,0.006432846,0.006731899
3065,0.006380863,0.006732532
3070,0.006461641,0.006743195
3075,0.006338263,0.006737863
3080,0.006430743,0.006751192
3085,0.00632229,0.006725162
3090,0.006428971,0.006738471
3095,0.006386188,0.00669766
3100,0.00638774,0.006708306
3105,0.006428971,0.006700967
3110,0.00638134,0.006698309
3115,0.006457336,0.00671629
3120,0.00638111,0.006703624
3125,0.006449358,0.006718952
3130,0.006365158,0.006700321
3135,0.006455563,0.006702983
3140,0.006343889,0.006692337
3145,0.006432671,0.006708306
3150,0.006322623,0.00667705
3155,0.006394614,0.006687679
3160,0.006297282,0.006659335
3165,0.006393041,0.00667705
3170,0.006306454,0.006649411
3175,0.006369399,0.006669752
3180,0.006373601,0.00665737
3185,0.006370481,0.006661577
3190,0.006395693,0.006660023
3195,0.006381053,0.006652064
3200,0.006412483,0.006649411
3205,0.006389863,0.006634201
3210,0.006420436,0.00665737
3215,0.006389863,0.006649411
3220,0.00642839,0.006644104
3225,0.006375767,0.006633492
3230,0.006393223,0.006618307
3235,0.006373124,0.006623605
3240,0.006375767,0.006614775
3245,0.00635131,0.006614775
3250,0.006381053,0.00659698
3255,0.006341844,0.00659698
3260,0.006366265,0.00659698
3265,0.006321643,0.006587282
3270,0.006358348,0.006577462
3275,0.006316604,0.006577462
3280,0.006374182,0.006577462
3285,0.006313739,0.006577462
3290,0.00638034,0.006577462
3295,0.006304692,0.006580103
3300,0.006366265,0.006595068
3305,0.006280946,0.006574821
3310,0.006381053,0.00660756
3315,0.006289741,0.006584637
3320,0.006360987,0.00659698")
Using first order difference equation (recursively calculated function) I calculated modeled value of y2_mod at each x.
Parameter that I wanted to fit is a (parameter b is fixed).
I used nls.lm function for fitting. This is how the code looks:
fitparms <- c(a = .008)
fn_residuals <- function(fitparms, data) {
b <- .00111
t <- data$x
dT <- t[2] - t[1]
N <- numeric(length(t))
y1_exp <- data$y1_exp
y2_mod <- numeric(length(t))
y2_mod[1] <- data$y2_exp[1]
for (h in seq_len(length(t)-1)) {
y2_mod[h+1] <- (fitparms[1] * (y1_exp[h+1] - y2_mod[h]) + b * (y1_exp[h+1] - y2_mod[h])) * dT + y2_mod[h]
}
data_2 <<- data %>%
add_column(y2_mod = y2_mod)
# residuals
ssqres <- data_2$y2_mod - data_2$y2_exp
# # return predicted vs experimental residual
return(ssqres)
}
# Ploting data
data_2 %>%
gather(y1_exp, y2_exp, y2_mod, key = "y", value = "value") %>%
ggplot(aes(x = x)) +
geom_line(aes(y = value, color = y))
# Fitting
fitval <- nls.lm(par = fitparms,
fn = fn_residuals,
lower = rep(0, 1), #defined lower limit for parameter
control = nls.lm.control(nprint = 1),
data = data)
This is my output:
It. 0, RSS = 0.00020291, Par. = 0.008
It. 1, RSS = 8.10975e-05, Par. = 0.00326637
It. 2, RSS = 7.9177e-05, Par. = 0.00349277
It. 3, RSS = 7.90245e-05, Par. = 0.00356081
It. 4, RSS = 7.90137e-05, Par. = 0.00357927
It. 5, RSS = 7.90129e-05, Par. = 0.00358413
It. 6, RSS = 7.90129e-05, Par. = 0.00358539
It. 7, RSS = 7.90129e-05, Par. = 0.00358572
It. 8, RSS = 7.90129e-05, Par. = 0.0035858
# Summary of fit
summary(fitval)
Parameters:
Estimate Std. Error t value Pr(>|t|)
a 3.586e-03 6.921e-05 51.81 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.000345 on 664 degrees of freedom
Number of iterations to termination: 8
Reason for termination: Relative error in the sum of squares is at most `ftol'.
# Estimated parameter
parest <- as.list(coef(fitval))
parest
$a
[1] 0.003585804
I am new to this field, so my questions are:
if this is the proper (right) way to solve such problems (is using nls.lm correct)?
Is there another better way of tackling the problem of finding optimal value for such curve fitting?
Also, I did not find any package that I could use for implementing difference equations for my kind of problem, i.e. I used y1_exp data at each x for calculation of y2_mod.
For example deSolve package has a special-purpose solver (method = "iteration") for discrete time models, difference equations (page 30 of Vignette: Solving Initial Value Differential Equations in R), however with this solver you can only input initial values (e.g.: y1_exp[1]).
So, is there a better way of writing a code for my kind difference equation problem, or does some package already exists that deals with
such problems?
Thanks! Jernej
I have not checked if everything is correct, but nls.lm is indeed one suitable way to go. Another, more advanced package with more flexibility is package FME.
You are right, package deSolve can also handle difference equations. Depending on how the equations are specified, one can either use method="euler" (if the model returns the difference) or method="iteration", if the model returns the new value. The model starts of course with the (vector of) first initial value(s), but if you want to force your model by an external input, you can use a forcing function, see ?forcings.
A possible approach could be to combine forcing with "iteration".
Thomas

Nonlinear least square in r

Check please, there is a good fit or not.
I have two vectors here:
x=c(30,110)
y=c(0.000760289, 0.000800320, 0.000830345, 0.000840353, 0.000860370,
0.000910414, 0.000990490, 0.001090594, 0.001200721, 0.001350912,
0.001531172, 0.001751533, 0.001961923, 0.002192402, 0.002463031,
0.002793899, 0.003185067, 0.003636604, 0.004148594, 0.004721127,
0.005394524, 0.006168989, 0.007014544, 0.007870894, 0.008758242,
0.009656474, 0.010565620, 0.011485709, 0.012396520, 0.013308162,
0.014271353, 0.015326859, 0.016525802, 0.017889059, 0.019447890,
0.021223636, 0.023145810, 0.025174229, 0.027391752, 0.029645106,
0.032337259, 0.035347424, 0.039375125, 0.043575783, 0.048003973,
0.052926206, 0.058307309, 0.064581189, 0.071947231, 0.080494476,
0.089891885, 0.100671526, 0.111971207, 0.124237571, 0.137975539,
0.153629149, 0.171239194, 0.190712664, 0.212079979, 0.235026373,
0.259457493, 0.282867017, 0.307830359, 0.334773680, 0.364001719,
0.395742526, 0.425596389, 0.458391314, 0.494558651, 0.534657357,
0.579354317, 0.616075034, 0.656680256, 0.701804548, 0.752133146,
0.808558032, 0.872226001, 0.944664487, 1.027837007, 1.124484096,
1.238426232)
and this My codes:
a=0.000066332
b=0.091394349
k=50.2552812
fit = nls(y ~ (a*b*exp(b*(x+0.5))*k)/((k*b)+(a*(exp(b*(x+0.5))-1))),
start=list(a=a,b=b, k=k))
plot(x,y,col="red", xlab="Age",ylab=expression(paste(mu)))
lines(x,predict(fit), col="blue")
Is this a good fitting ? could I also use the maximum likelihood estimation for this x and y to estimate a, b, and k ? Could give me some codes for doing MLE in r ? I need for comparing between NLS and MLE.
Really appreaciate for the helps. Thank you

Resources