I have a bunch of data and I need to find the parameter values for this equation that fit the data.
I am a complete beginner to R by the way... How do I use the nls function properly?
values <- read.csv(file.choose())
nls(y~A/cos(B*(C + x))^2 + D,
data =values, start = c(A = 1, B=1, C=0, D=0))
error
Error in nls(y ~ A/cos(B * (C + x))^2 + D, data = values, start = c(A = 1, :
step factor 0.000488281 reduced below 'minFactor' of 0.000976562
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
data
structure(list(X = c(212.8, 219.12, 226.07, 232.39, 239.97, 247.55,
254.5, 262.4, 269.67, 276.94, 283.89, 289.89, 297.15, 303.79,
310.11, 317.06, 322.75, 329.06, 335.38, 341.07, 347.7, 353.71,
359.39, 365.08, 371.71, 376.45, 382.77, 388.77, 394.46, 400.78,
406.78, 412.78, 419.1, 425.27, 431.27, 437.59, 442.96, 448.97,
454.65, 460.34, 465.39, 470.77, 477.08, 482.14, 486.56, 492.25,
497.3, 502.36, 507.41, 512.47, 517.52, 522.89, 528.58, 533.95,
539.32, 545.32, 550.7, 555.75, 561.44, 567.12, 571.86, 576.92,
581.97, 587.03, 605.67, 611.04, 620.2, 624.94, 643.58, 648.95,
658.43, 663.48, 673.28, 683.38, 688.12, 693.18, 697.92, 702.02,
706.45, 711.5, 715.61, 720.35, 724.14, 737.09, 742.15, 746.89,
750.99, 756.05, 760.16, 774.43, 779.17, 788.02, 797.5, 801.6,
810.45, 814.87, 823.09, 831.93, 840.46, 849.31, 862.58, 866.68,
871.42, 880.59, 885.01, 894.8, 907.44, 916.92, 925.13, 937.45,
949.77, 958.94, 972.2, 981.68, 991.16, 1000.32, 1018.96, 1031.92,
1044.87, 1057.82, 1066.98, 1080.88, 1096.05, 1109.96, 1120.39,
1129.55, 1138.71, 1148.19, 1157.35, 1166.2, 1176.31, 1186.1,
1196.21, 1207.27, 1223.38, 1239.49, 1249.92, 1265.4, 1275.19,
1290.67, 1301.73, 1312.79, 1325.11, 1336.48, 1348.8, 1359.86,
1372.5, 1378.5, 1390.19, 1401.88, 1413.57, 1424.63, 1437.58,
1449.65, 1463.23, 1476.5, 1490.72, 1504.62, 1518.84, 1525.79,
1532.42, 1539.06, 1552.01, 1565.91, 1586.76, 1600.98, 1632.89,
1658.48, 1666.69, 1674.27, 1683.12, 1692.28, 1700.5, 1718.5,
1727.03, 1735.25, 1745.99, 1754.2, 1763.68, 1773.78), Y = c(-806.78,
-805.83, -804.89, -804.25, -802.36, -801.73, -800.78, -799.83,
-797.94, -796.67, -795.41, -795.09, -793.83, -792.88, -792.25,
-791.3, -790.35, -789.72, -788.77, -788.14, -786.88, -786.25,
-785.61, -784.03, -783.4, -782.77, -782.14, -781.19, -780.56,
-780.24, -779.61, -778.66, -777.72, -777.47, -776.52, -775.89,
-775.26, -774.31, -773.68, -772.73, -772.1, -771.78, -771.78,
-771.15, -769.89, -769.26, -768.31, -768.31, -767.68, -767.36,
-767.04, -766.1, -765.47, -765.47, -764.52, -764.2, -763.57,
-762.94, -762.31, -761.67, -761.36, -761.04, -760.41, -760.09,
-758.83, -758.51, -756.94, -756.62, -755.99, -755.36, -754.72,
-754.09, -753.46, -751.88, -752.51, -752.51, -751.88, -751.56,
-751.56, -751.25, -751.25, -750.3, -750.3, -750.3, -749.67, -749.67,
-749.67, -749.04, -748.72, -748.15, -748.15, -747.52, -747.2,
-747.2, -746.89, -746.89, -746.89, -746.89, -746.25, -745.94,
-746.25, -746.57, -745.94, -745.62, -745.31, -745.31, -745.31,
-744.67, -745.31, -745.62, -745.62, -745.62, -746.25, -745.62,
-746.25, -746.25, -747.2, -747.2, -747.83, -748.47, -748.78,
-749.41, -750.04, -750.14, -751.41, -751.41, -752.36, -752.99,
-753.3, -754.25, -754.88, -754.88, -756.15, -756.78, -758.04,
-759.62, -760.57, -762.47, -763.41, -764.04, -764.99, -766.26,
-767.84, -769.1, -770.05, -771, -772.57, -773.21, -774.47, -776.37,
-777.63, -778.58, -780.47, -781.77, -783.35, -785.25, -787.14,
-788.41, -790.3, -790.94, -791.88, -793.15, -794.73, -796.94,
-799.47, -801.68, -806.42, -810.21, -811.15, -813.05, -814.31,
-815.58, -817.47, -819.37, -821.58, -822.53, -824.42, -826.32,
-827.58, -829.36)), class = "data.frame", row.names = c(NA, -180L
))
Theres the data... SO is yelling at me because there is too much code so I need to type some more words to make it less angry at me.
With the data supplied I get an error because of the lack of equality of "x" and "X", and likewise "y" and "Y". Fixing that allows the function to run with out error:
> nls(y~A/cos(B*(C + x))^2 + D,
+ data =values, start = c(A = 1, B=1, C=0, D=0))
Error in nls(y ~ A/cos(B * (C + x))^2 + D, data = values, start = c(A = 1, :
parameters without starting value in 'data': y, x
> str(values)
'data.frame': 180 obs. of 2 variables:
$ X: num 213 219 226 232 240 ...
$ Y: num -807 -806 -805 -804 -802 ...
> nls(Y~A/cos(B*(C + X))^2 + D,
+ data =values, start = c(A = 1, B=1, C=0, D=0))
Nonlinear regression model
model: Y ~ A/cos(B * (C + X))^2 + D
data: values
A B C D
1.871e-04 1.000e+00 -2.615e-02 -7.713e+02
residual sum-of-squares: 86758
Number of iterations to convergence: 10
Achieved convergence tolerance: 9.838e-07
And the fit of that model is just as bad as the fit of the model offered by Dave2e. The data looks seriously parabolic: