I am trying to fit some data to a nonlinear least squares regression in R. I have two factors I would like to include in the model, one with two levels, and one with four levels. The one with two levels has a baseline level. However, I need all 4 levels in the second factor because there is no "baseline" for that one.
I have tried making dummy variables and including them in the model, but I keep getting this error: Error in nlsModel(formula, mf, start, wts) : singular gradient matrix at initial parameter estimates
This is the formula I am trying to fit: y ~ m*x/(x+k)
I tried including the extra variables with plus signs and a dummy variable: y ~ m*x/(x+k) + b1 * f1 + b2 * f2c1 + b3 * f2c2 + b4 * f2c3 + b5 * f2c4
I am using f1 and f2 as factors 1 and 2 and c1-4 as conditions 1-4 for factor 2.
I am giving the formula y and x values, and I am solving for m and k.
data: I have renamed variables to make this simpler. Not really sure of another dataset that resembles mine, but if you know of one let me know so I can reference that instead.
df <- structure(list(x = c(10, 10, 10, 19, 19, 19, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030, 1, 1, 1, 10, 10, 10, 19, 19, 19, 48, 48, 48, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030, 10, 10, 10, 19, 19, 19, 48, 48, 48, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030, 10, 10, 10, 19, 19, 19, 48, 48, 48, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030, 10, 10, 10, 48, 48, 48, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030, 10, 10, 10, 19, 19, 19, 48, 48, 48, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030, 1, 1, 1, 10, 10, 10, 19, 19, 19, 48, 48, 48, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030, 1, 1, 1, 10, 10, 10, 19, 19, 19, 48, 48, 48, 100, 100, 100, 171, 171, 171, 310, 310, 310, 1551, 1551, 1551, 3030, 3030, 3030), y = c(0.545069304073641, 0.498474757641486, 0.477049918346262, 0.520649496743, 0.482516888454425, 0.468532243616422, 0.54456653126309, 0.568737608386396, 0.499609210017092, 0.527910677510106, 0.490805829415139, 0.549374309653943, 0.52446189534141, 0.537082540976272, 0.52024629099236, 0.486377463477651, 0.566462390676463, 0.609697363043081, 0.504844388790332, 0.537130349800457, 0.489065617952382, 0.197542910770175, 0.160513521094901, 0.213144576302472, 0.382595061660297, 0.442388084442234, 0.431039189061169, 0.501821920123504, 0.419326882916461, 0.450305171376138, 0.54374504318749, 0.434165332773468, 0.529959769945767, 0.478107780251134, 0.496531576348917, 0.492093729217865, 0.47007534390937, 0.484674230762311, 0.544786827749162, 0.496636771591611, 0.513157723267727, 0.477085056164907, 0.457863125942786, 0.505850611449622, 0.479883783054374, 0.415274046611927, 0.470513274260385, 0.488389942144232, 0.46835115751543, 0.450001680279235, 0.474148991759112, 0.48350871575944, 0.463280419327448, 0.45380548076251, 0.523584450774162, 0.495087610596011, 0.488858228455294, 0.493099325232702, 0.527996108320428, 0.514968754699488, 0.578841686687416, 0.481721164509045, 0.515689839945108, 0.504664425165595, 0.523736107799738, 0.524903978114413, 0.493518375348605, 0.506439963776571, 0.534748504808894, 0.523319963512947, 0.534546444846738, 0.541583634178305, 0.462347390610192, 0.495132490870331, 0.479766546931413, 0.486532578975367, 0.444408974433802, 0.48943629828167, 0.468861401095353, 0.540535905818436, 0.499797290027505, 0.515052105827494, 0.566866868262285, 0.503521622041808, 0.553131236296148, 0.512076769160108, 0.547774678283411, 0.518936081361654, 0.529618298799362, 0.51032521471201, 0.520598985166483, 0.568373713880407, 0.512414683328682, 0.523679193166562, 0.521290180003916, 0.53184505880791, 0.424247647910926, 0.37951081435601, 0.407560483989135, 0.452433325325847, 0.497763201874307, 0.417960016312402, 0.541372816985466, 0.576073035399456, 0.449540922906708, 0.438450170598359, 0.442357998333277, 0.601884491577966, 0.574139464973075, 0.536766579586688, 0.500560245849426, 0.502419653868863, 0.461464157074795, 0.603458251922098, 0.544958178514559, 0.549944449984256, 0.535035012599522, 0.341863462448256, 0.409509013052461, 0.365337439548206, 0.40574295727311, 0.545404275532816, 0.391829233802997, 0.499704394462313, 0.476108279456204, 0.509727430067993, 0.470089530154881, 0.491737596484615, 0.683973868609802, 0.461452829957335, 0.436865348663681, 0.578685176655528, 0.605201676010679, 0.520055331329122, 0.559637997837225, 0.597841280659308, 0.633413148745386, 0.651861417707689, 0.569154355415896, 0.622417325261126, 0.598855191621442, 0.311147693289223, 0.230440435446028, 0.215907479043353, 0.461673088666706, 0.447728457373554, 0.479920582707447, 0.50700180831017, 0.471579447500711, 0.472982507093927, 0.517641474654853, 0.438071643928347, 0.431337115358753, 0.475952783865343, 0.452564740484902, 0.49656759937233, 0.493146920027774, 0.498247898383314, 0.474109028023533, 0.461915239913428, 0.438559211729778, 0.478380852248177, 0.41162661594861, 0.472412208973243, 0.415435474910108, 0.450485163658403, 0.512526112972884, 0.529886967702089, 0.366667256538505, 0.235136581592252, 0.311673948972614, 0.455141658719897, 0.425921900701394, 0.422762262690172, 0.418949933768408, 0.439403391622533, 0.4499477014617, 0.467435433511363, 0.401543209144888, 0.463977155464223, 0.484076442360687, 0.484741208661163, 0.486359376392174, 0.433399968169649, 0.448225223681549, 0.441554694636448, 0.552750322498672, 0.50461315180706, 0.485098429387248, 0.432019940283289, 0.484181718214331, 0.473506553658715, 0.489282284413696, 0.452933014286251, 0.443812898399205 ), f1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), f2c4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195"))
Using the minpack.lm package with this formula:
library(minpack.lm)
nlsfit <- nlsLM(
formula = y ~ m*x/(x+k) + b1 * f1 + b2 * f2c1 + b3 * f2c2 + b4 * f2c3 + b5 * f2c4,
data = df,
control = nls.lm.control(
ftol = 1e-10,
ptol = 1e-10,
maxiter = 280
),
start = list(
m = 0.5,
k = 2,
b1 = 0,
b2 = 0,
b3 = 0,
b4 = 0,
b5 = 0)
)
Ultimately, I would like to do comparisons between each grouping, taking into account factor combinations. E.g. see how the combination of factors 1 and 2 change the values m and k, respectively.
sessionInfo(): R version 4.2.2 (2022-10-31) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Ventura 13.1