5
$\begingroup$

I have a scatter plot and I am trying to get the equation of a line that best fits the bottom bound of the data.

I started by taking some guesses for the best form of the boundary equation and used the FindFit[] command to test out each. Once I found a good shape I used it to split the data and create a subset of the data that was closest to the bottom boundary. I then used the FindFit[] command again with the same form for the equation and got a line that more closely fit the bottom boundary.

When I took the sum of the squares error for this line with my data I got a number over 100 which isn't great. Is there a better way to do this? I tried to follow what I found in these posts here and here and I was unsuccessful. Any suggestions are appreciated.

The code for my data to fit and resulting fit line are as follows.

datatofit = {{6.737172338, 3.363690623}, {7.050660212, 
3.373736489}, {7.045260043, 2.970956386}, {7.029287617, 
2.9525655}, {7.004950478, 2.879583858}, {6.974853608, 
2.86146603}, {6.941458132, 2.818494481}, {6.906952777, 
2.893338455}, {6.872201926, 2.940551101}, {6.837925451, 
3.397634859}, {6.803204973, 3.552844431}, {6.766544775, 
3.244249901}, {6.71850745, 3.302987412}, {6.684253549, 
3.167875865}, {6.66670728, 3.150775565}, {6.654886001, 
3.152303268}, {6.647275038, 3.151626188}, {6.643345749, 
3.151580709}, {6.64064312, 3.151574041}, {7.046091938, 
3.120075363}, {7.040626586, 2.771751037}, {7.024372252, 
2.799740928}, {6.999594641, 2.669548897}, {6.968712172, 
2.676528958}, {6.934228877, 2.624395294}, {6.898397676, 
2.652516685}, {6.861701263, 3.106205768}, {6.824856341, 
3.256504668}, {6.786708972, 3.083377532}, {6.744894076, 
3.219777785}, {6.68699347, 3.464187669}, {6.644085885, 
3.056803258}, {6.621036724, 3.03776599}, {6.60495429, 
3.03613608}, {6.593188858, 3.028994499}, {6.584495417, 
3.032142225}, {6.578697835, 3.032760756}, {7.043005505, 
2.879433241}, {7.037448284, 2.673586176}, {7.021021612, 
2.731079036}, {6.995891975, 2.566450694}, {6.964546514, 
2.600742648}, {6.92940586, 2.532713837}, {6.892545224, 
2.547549465}, {6.854596368, 2.771142903}, {6.816023846, 
3.123130369}, {6.775368031, 3.093750555}, {6.7299056, 
3.183432893}, {6.664947098, 3.667093691}, {6.615699834, 
2.983366728}, {6.588638333, 3.392473696}, {6.569315509, 
2.950492375}, {6.554261586, 2.941110252}, {6.542808184, 
2.941959397}, {6.533554849, 2.93893594}, {7.040691711, 
2.698683379}, {7.035116631, 2.621157784}, {7.018574159, 
2.700002587}, {6.993203755, 2.511981159}, {6.961459733, 
2.560957061}, {6.925824728, 2.487887417}, {6.888304185, 
2.509698731}, {6.849403353, 3.013369851}, {6.809502199, 
3.030071466}, {6.767022251, 3.453330064}, {6.718737756, 
3.093764566}, {6.648469337, 3.580313549}, {6.593848593, 
2.877026086}, {6.563586948, 3.152084598}, {6.541449976, 
2.830720899}, {6.524255063, 2.827916495}, {6.510411715, 
2.826588059}, {6.498860923, 2.824406461}, {7.038955285, 
2.578493867}, {7.033340199, 2.612255811}, {7.016687959, 
2.735690273}, {6.991143306, 2.508233093}, {6.959143325, 
2.555844374}, {6.923117192, 2.49172362}, {6.88506039, 
2.536450569}, {6.845406911, 3.071325015}, {6.804569678, 
3.193536334}, {6.760718398, 3.28270189}, {6.710350047, 
2.531280794}, {6.635755922, 3.20519061}, {6.57698951, 
2.26708585}, {6.543796226, 2.457274245}, {6.519703559, 
2.229251888}, {6.500484118, 2.68381051}, {6.484778548, 
2.226266777}, {6.471324049, 2.229252698}, {7.037601367, 
2.5107129}, {7.031963505, 2.564917697}, {7.015230995, 
2.700816343}, {6.989544414, 2.815891687}, {6.957335874, 
2.507864027}, {6.920995503, 2.851306634}, {6.882559183, 
2.500670143}, {6.842343633, 2.927637977}, {6.800739526, 
2.597890463}, {6.755802639, 3.059142293}, {6.703736537, 
2.526782669}, {6.625558242, 2.932715688}, {6.563584803, 
2.196284669}, {6.528268014, 2.276971557}, {6.502230053, 
2.142517738}, {6.481350429, 2.280845624}, {6.464057366, 
2.127040407}, {6.449274977, 3.210672349}, {7.036520362, 
2.48691801}, {7.030859594, 2.567515379}, {7.014069926, 
2.658174137}, {6.988285965, 2.773057218}, {6.955909261, 
2.509486114}, {6.919324165, 2.811164587}, {6.880558873, 
2.524767089}, {6.839891932, 2.917997801}, {6.797684653, 
2.617454777}, {6.751920865, 3.12821285}, {6.698498976, 
2.265103094}, {6.617668027, 2.851725101}, {6.552843345, 
1.914902384}, {6.515720946, 1.997611706}, {6.488148018, 
1.858929325}, {6.465919878, 1.961247735}, {6.447365016, 
1.841455432}, {6.431261185, 1.991596183}, {7.035653479, 
2.441673234}, {7.029979193, 2.642091664}, {7.013143124, 
2.779600057}, {6.987259413, 2.655408023}, {6.954754304, 
2.747056288}, {6.917982547, 2.690503028}, {6.878965321, 
2.671670335}, {6.837984295, 2.792041578}, {6.795288058, 
2.699023107}, {6.748789425, 2.994343796}, {6.69434619, 
2.75154734}, {6.611333377, 2.416987663}, {6.544183314, 
1.670533864}, {6.505492489, 1.745627845}, {6.4767104, 
1.592734733}, {6.453310046, 1.646655847}, {6.433635399, 
1.566881917}, {6.416503415, 1.6386405}, {7.034954678, 
2.430667632}, {7.029269788, 2.655392115}, {7.012381848, 
2.779696857}, {6.986424477, 2.677937026}, {6.953816738, 
2.767393148}, {6.916904312, 2.738390317}, {6.877678574, 
2.521709116}, {6.836410168, 2.903247296}, {6.793320608, 
2.691670804}, {6.746267116, 2.973871121}, {6.690902848, 
2.8373169}, {6.606132978, 2.004034878}, {6.537142696, 
1.358901692}, {6.497198187, 1.492049861}, {6.467300528, 
1.202542717}, {6.443067018, 1.278133712}, {6.422348331, 
1.139119465}, {6.404380246, 1.193699038}, {7.034360231, 
2.423314873}, {7.028676326, 2.546343642}, {7.011758013, 
2.70421933}, {6.985764669, 2.732327838}, {6.953065808, 
2.492944411}, {6.916027603, 2.828091797}, {6.87662091, 
2.502793251}, {6.835117845, 2.956424635}, {6.791725157, 
2.785362746}, {6.744227722, 2.725070054}, {6.688177644, 
2.952462805}, {6.601925407, 1.871037226}, {6.531483793, 
3.299572125}, {6.490419284, 1.205062821}, {6.459686231, 
0.924268761}, {6.43460432, 1.072051642}, {6.41317736, 
0.799702673}, {6.394433027, 0.874613658}, {7.033887306, 
2.414621979}, {7.028191073, 2.584108445}, {7.011251342, 
2.677415351}, {6.98520808, 2.591920501}, {6.952431456, 
2.728131626}, {6.915295189, 2.606394808}, {6.875752864, 
2.764029792}, {6.834058649, 2.629637742}, {6.790420478, 
3.07524121}, {6.742551507, 2.963775838}, {6.685933428, 
3.052845912}, {6.598483391, 1.771794681}, {6.526679942, 
3.711712717}, {6.484933022, 1.110138892}, {6.453429566, 
2.880676607}, {6.427598138, 0.827650948}, {6.405553788, 
0.631667532}, {6.386130329, 0.760793308}, {7.033485774, 
2.424465446}, {7.027784029, 2.542896896}, {7.010825267, 
2.676392444}, {6.984727707, 2.426663261}, {6.951895818, 
2.503322157}, {6.914674556, 2.668239461}, {6.87504636, 
2.71191227}, {6.833198996, 2.515308877}, {6.789335686, 
2.708438633}, {6.741153283, 2.490874554}, {6.684096853, 
3.032019326}, {6.59566427, 1.686277852}, {6.522838247, 
2.919478967}, {6.480288152, 0.986717582}, {6.448176753, 
2.068486954}, {6.421847708, 0.625051368}, {6.39926062, 
1.922526712}, {6.379303182, 0.356377592}, {7.033156909, 
2.411926653}, {7.027450152, 2.541483248}, {7.010464596, 
2.678526802}, {6.984341108, 2.740824687}, {6.951478592, 
2.794615538}, {6.914169479, 2.558769959}, {6.874430575, 
2.843349391}, {6.832468962, 2.465629313}, {6.788452692, 
2.980813388}, {6.740044845, 2.796268712}, {6.682518511, 
2.973088953}, {6.593055138, 1.602870812}, {6.519532175, 
2.67604757}, {6.476504456, 0.922451616}, {6.44392189, 
1.551595069}, {6.417042066, 0.524349666}, {6.394007742, 
1.335879104}, {6.373604641, 0.204979694}, {7.032874567, 
2.410828768}, {7.027159736, 2.607313012}, {7.010167791, 
2.626230999}, {6.984022698, 2.525194094}, {6.951121482, 
2.676686105}, {6.913762616, 2.432960144}, {6.873943192, 
2.602109532}, {6.831868118, 2.424174076}, {6.787698864, 
2.833240042}, {6.739079714, 2.429085504}, {6.681285512, 
3.080801999}, {6.516860422, 2.105024931}, {6.473298911, 
0.874160363}, {6.440230786, 1.285081302}, {6.413106094, 
0.456291156}, {6.389685571, 0.973943373}, {6.368894179, 
0.098754837}, {7.032645935, 2.405591528}, {7.026910561, 
2.372253613}, {7.009913701, 2.487133006}, {6.983737617, 
2.364848558}, {6.950791466, 2.524420007}, {6.91340328, 
2.364589563}, {6.873512579, 2.823145133}, {6.831364697, 
2.400296221}, {6.787055213, 2.679382786}, {6.738285943, 
2.409901849}, {6.68016282, 2.899877109}, {6.514641322, 
1.9369223}, {6.470610335, 0.836101475}, {6.437300126, 
1.129164882}, {6.409790246, 0.405443991}, {6.38602318, 
0.686981094}, {6.364910446, 0.028242141}, {7.032440554, 
2.405579999}, {7.026721599, 2.360197275}, {7.009694316, 
2.478204395}, {6.983510617, 2.348796858}, {6.950549463, 
2.522441741}, {6.913104044, 2.350943474}, {6.873169546, 
2.634773752}, {6.830937015, 2.392838217}, {6.786543993, 
2.839201002}, {6.737598664, 2.397235599}, {6.679324934, 
2.832339738}, {6.512784374, 1.713420067}, {6.468415156, 
0.803487782}, {6.43476251, 1.036945302}, {6.40702598, 
0.362975169}, {6.382961843, 
0.599796071}, {6.361672483, -0.033748511}, {7.032269629, 
2.404195798}, {7.026546953, 2.311238526}, {7.009518105, 
2.44643103}, {6.983325046, 2.315294417}, {6.950301954, 
2.511458327}, {6.912841213, 2.336852323}, {6.872860057, 
2.641195519}, {6.830569453, 2.385042086}, {6.786088247, 
2.688150649}, {6.737051299, 2.385137509}, {6.678553276, 
2.699520498}, {6.511231072, 1.604350357}, {6.466562622, 
0.77860391}, {6.432747844, 1.093028664}, {6.404695153, 
0.331838296}, {6.380443334, 
0.610163948}, {6.358866139, -0.073129957}, {7.021387848, 
3.541701984}, {7.017318549, 3.366268739}, {7.004005224, 
3.379362555}, {6.983608703, 3.310765727}, {6.957981545, 
3.372186381}, {6.9300328, 3.238677971}, {6.901310421, 
3.513454512}, {6.873579569, 3.38958335}, {6.84697077, 
3.448019292}, {6.822146907, 3.672250607}, {6.797997064, 
3.421691746}, {6.773159504, 3.408306056}, {6.755629533, 
3.409602569}, {6.746650751, 3.405713642}, {6.742636776, 
3.405360568}, {6.74038778, 3.405358496}, {6.738733887, 
3.405358717}, {6.737366301, 3.405358714}, {7.009119349, 
3.333399632}, {7.004912836, 2.99492421}, {6.990923136, 
2.974801307}, {6.969367763, 2.896208713}, {6.942122197, 
2.879163429}, {6.91185393, 2.820121984}, {6.880016883, 
2.965103286}, {6.848151481, 2.961425017}, {6.81626398, 
3.386735289}, {6.784651895, 3.441177003}, {6.751090931, 
3.280806418}, {6.712540883, 3.506529359}, {6.682764581, 
3.234167522}, {6.665778087, 3.219308724}, {6.654539923, 
3.220829707}, {6.647345542, 3.220185319}, {6.643545231, 
3.220137592}, {6.641009464, 3.220132845}, {7.00275519, 
3.104026368}, {6.998431434, 2.788884407}, {6.984162424, 
2.806069462}, {6.962097894, 2.686021977}, {6.93390774, 
2.681217719}, {6.902432022, 2.632561023}, {6.868884985, 
2.670789886}, {6.834996486, 3.073172065}, {6.800221064, 
3.278592264}, {6.764769929, 3.217612391}, {6.726026299, 
3.234278978}, {6.679223478, 3.566967796}, {6.642084036, 
3.132144621}, {6.620080487, 3.116832626}, {6.604457483, 
3.115273601}, {6.592882502, 3.108860221}, {6.584419577, 
3.111700897}, {6.578710123, 3.112401791}, {6.998453514, 
2.884078687}, {6.994040671, 2.676144008}, {6.979589633, 
2.722120393}, {6.957083533, 2.575196145}, {6.928331939, 
2.598627047}, {6.895995935, 2.541203344}, {6.861383373, 
2.557500226}, {6.826040369, 2.772999508}, {6.789260743, 
3.149417027}, {6.751169366, 3.225516519}, {6.708668456, 
3.19683746}, {6.655960276, 3.63032235}, {6.613106365, 
3.074207024}, {6.587176578, 3.395948961}, {6.568288904, 
3.045830522}, {6.553591418, 3.037174422}, {6.542342907, 
3.037655551}, {6.533478246, 3.034637296}, {6.995257836, 
2.710463265}, {6.990840026, 2.612485842}, {6.976156825, 
2.681489905}, {6.95344572, 2.513754005}, {6.924218623, 
2.557578841}, {6.891257737, 2.496164396}, {6.855802202, 
2.516896036}, {6.819340411, 3.03692985}, {6.781177105, 
3.053046016}, {6.741178178, 3.155774023}, {6.695804347, 
3.098114561}, {6.638516975, 3.342320661}, {6.590926409, 
2.992880091}, {6.561614268, 3.240979384}, {6.540231203, 
2.947801818}, {6.523550665, 2.945132883}, {6.509802935, 
2.943379145}, {6.498535038, 2.940797565}, {6.99284548, 
2.571171245}, {6.988381018, 2.538146306}, {6.973591929, 
2.708241023}, {6.950678304, 2.439909927}, {6.921097543, 
2.504480264}, {6.88768125, 2.876971639}, {6.851603753, 
2.472316264}, {6.814325066, 2.797995352}, {6.775012875, 
2.718968741}, {6.733477612, 2.880545781}, {6.685861511, 
2.172919922}, {6.624807712, 2.797501221}, {6.573655321, 
1.823490126}, {6.541745821, 2.163771856}, {6.518234088, 
1.74777426}, {6.499406713, 2.52076469}, {6.484056326, 
1.726976337}, {6.470895399, 1.71411499}, {6.990983255, 
2.492374661}, {6.986505805, 2.521507171}, {6.971605514, 
2.691000161}, {6.94848282, 2.423092642}, {6.918675658, 
2.483312647}, {6.884913274, 2.726465955}, {6.848353042, 
2.472908418}, {6.810431393, 2.730279131}, {6.770206766, 
2.592288305}, {6.727600214, 2.86105508}, {6.67812493, 
2.257416686}, {6.614139375, 2.606553397}, {6.559854955, 
1.926487703}, {6.525922737, 2.04205007}, {6.500692551, 
1.858306873}, {6.4801801, 2.052948153}, {6.463153087, 
1.836944653}, {6.448645105, 3.186252252}, {6.989486885, 
2.447788691}, {6.984961003, 2.51823223}, {6.970017249, 
2.621197985}, {6.946754224, 2.654136753}, {6.916724071, 
2.722538609}, {6.882707695, 2.660782622}, {6.845750492, 
2.486855827}, {6.807332509, 2.705607007}, {6.766441258, 
2.616313927}, {6.722798257, 2.789726234}, {6.671931189, 
2.266686188}, {6.605619456, 2.582709675}, {6.548886496, 
1.971415758}, {6.513142743, 2.042152732}, {6.486316292, 
1.908717509}, {6.464518697, 1.996837852}, {6.446212182, 
1.889947327}, {6.430375826, 2.015387471}, {6.988286989, 
2.442797784}, {6.983753986, 2.523709108}, {6.968764455, 
2.815435814}, {6.945394744, 2.696070989}, {6.915228657, 
2.571777841}, {6.880977312, 2.736142572}, {6.843719044, 
2.625985493}, {6.804812407, 2.846609949}, {6.763408347, 
2.702631955}, {6.719175614, 2.725144312}, {6.666998881, 
2.812838126}, {6.598727966, 2.217421041}, {6.539968152, 
1.68036073}, {6.502715394, 1.753586116}, {6.47470821, 
1.609945838}, {6.451696211, 1.661851878}, {6.432379608, 
1.585905749}, {6.415510904, 1.653235924}, {6.987325382, 
2.428485538}, {6.982762868, 2.520642611}, {6.967719299, 
2.812619236}, {6.944097527, 2.716088442}, {6.913954197, 
2.543537804}, {6.87951661, 2.782685466}, {6.842022117, 
2.524040008}, {6.802835042, 2.960676849}, {6.761005457, 
2.699207732}, {6.715960506, 2.649555904}, {6.663108202, 
2.937761994}, {6.593322988, 1.891491441}, {6.532799258, 
1.360130197}, {6.494335951, 1.493722149}, {6.4652259, 
1.215214833}, {6.441309979, 1.286996334}, {6.420946513, 
1.155820892}, {6.403239809, 1.20877977}, {6.986517798, 
2.418721529}, {6.981959967, 2.516103273}, {6.966871231, 
2.707231238}, {6.943365135, 2.418431508}, {6.912955104, 
2.481041434}, {6.878336136, 2.864718778}, {6.84063932, 
2.493227818}, {6.80119304, 3.043388324}, {6.758959301, 
2.777887654}, {6.713480217, 2.578313592}, {6.659925271, 
3.0554261}, {6.588805235, 1.767714977}, {6.526893155, 
3.45736794}, {6.48731299, 1.206367522}, {6.457408188, 
0.945768081}, {6.432749806, 1.0869155}, {6.411664902, 
0.833238974}, {6.393075558, 0.900365879}, {6.985868568, 
2.39792015}, {6.981309469, 2.448437951}, {6.966163453, 
2.532392349}, {6.942630126, 2.462298954}, {6.912118892, 
2.711297056}, {6.877386636, 2.492756468}, {6.839524414, 
2.779518238}, {6.799848732, 2.562641964}, {6.757354724, 
3.094399317}, {6.711435272, 2.62743483}, {6.657060781, 
3.174913746}, {6.585146237, 1.688094305}, {6.521969739, 
3.41364081}, {6.48164058, 1.13711006}, {6.450958698, 
2.846539242}, {6.425646606, 0.896185308}, {6.403861401, 
0.744364294}, {6.384737069, 0.845871862}, {6.985337571, 
2.399150046}, {6.980746124, 2.606605224}, {6.965589764, 
2.677156504}, {6.941988084, 2.639323645}, {6.911420561, 
2.724689307}, {6.876596508, 2.702502904}, {6.838626888, 
2.812464811}, {6.798769366, 2.632561509}, {6.755966001, 
2.737586457}, {6.70970576, 2.511833139}, {6.654946343, 
2.978520481}, {6.581822314, 1.600239559}, {6.517952253, 
2.712632523}, {6.476923884, 0.981958276}, {6.445659866, 
2.035336396}, {6.419767371, 0.643196925}, {6.397498621, 
1.944480114}, {6.377793288, 0.403246413}, {6.98488373, 
2.405672661}, {6.980285513, 2.506241622}, {6.965115695, 
2.68036809}, {6.941468071, 2.766335724}, {6.91082076, 
2.853747521}, {6.875961946, 2.567132928}, {6.837833066, 
2.885985043}, {6.797828896, 2.459373833}, {6.754837292, 
2.742977197}, {6.708347546, 2.42044813}, {6.653122793, 
2.906196311}, {6.579265379, 1.526810823}, {6.514604203, 
2.500879394}, {6.473015497, 0.905592505}, {6.441240019, 
1.525323353}, {6.414919186, 0.515710199}, {6.392173836, 
1.320588181}, {6.372018259, 0.205196385}, {6.984495737, 
2.401439557}, {6.979931063, 2.627776046}, {6.964701582, 
2.672227083}, {6.941031686, 2.539974864}, {6.910324882, 
2.717804569}, {6.875406259, 2.436626084}, {6.837170521, 
2.601983053}, {6.797076282, 2.411391368}, {6.753928413, 
2.862968749}, {6.707237176, 2.387636149}, {6.651656207, 
2.949589832}, {6.511835988, 1.988418613}, {6.469758781, 
0.855749384}, {6.437618252, 1.281756011}, {6.410839786, 
0.445009267}, {6.387699425, 0.953176956}, {6.36717515, 
0.094736261}, {6.984166098, 2.394056378}, {6.979581812, 
2.51976677}, {6.964358662, 2.578668676}, {6.940664454, 
2.50945657}, {6.909937511, 2.657961257}, {6.874914928, 
2.621988611}, {6.836650401, 2.630219135}, {6.796406011, 
2.617593723}, {6.753107947, 2.617818296}, {6.706181283, 
2.610073938}, {6.650287374, 2.776940244}, {6.509566677, 
1.856344851}, {6.467052204, 0.816069506}, {6.434436932, 
1.113129647}, {6.407397081, 0.389667533}, {6.383938828, 
0.675287851}, {6.36320708, 0.013796113}, {6.983901887, 
2.394277751}, {6.979300989, 2.350943153}, {6.964083028, 
2.442414337}, {6.940335188, 2.34079414}, {6.909601437, 
2.534763847}, {6.874551906, 2.341245184}, {6.836169757, 
2.65065494}, {6.795903763, 2.373474174}, {6.752482957, 
2.894138331}, {6.705386817, 2.323014233}, {6.649273702, 
2.888122523}, {6.507589265, 1.655459882}, {6.464839239, 
0.784409443}, {6.431964994, 1.021987115}, {6.404557351, 
0.349725216}, {6.380898844, 
0.585498495}, {6.359823197, -0.042474931}, {6.983659377, 
2.392955434}, {6.97906581, 2.301237496}, {6.963837288, 
2.396246737}, {6.94008025, 2.306048454}, {6.909340835, 
2.524239683}, {6.874201695, 2.325594093}, {6.8358115, 
2.656288849}, {6.795409868, 2.364834796}, {6.751950885, 
2.692288624}, {6.704754512, 2.300977942}, {6.648361564, 
2.635953782}, {6.505973336, 1.572561081}, {6.462881392, 
0.758794965}, {6.429805958, 1.083726553}, {6.402195244, 
0.3172726}, {6.378271874, 
0.591334001}, {6.356973385, -0.084955223}, {6.992214922, 
3.176493752}, {6.988810316, 3.282279458}, {6.977223644, 
3.329380539}, {6.959304463, 3.228229582}, {6.936189756, 
3.344283466}, {6.911058348, 3.203824492}, {6.885410299, 
3.440714169}, {6.859699948, 3.404569564}, {6.835646283, 
3.394310454}, {6.813240258, 3.673407136}, {6.791815033, 
3.375189449}, {6.770521883, 3.36525816}, {6.755107451, 
3.366630444}, {6.746585121, 3.363286485}, {6.742720128, 
3.362921189}, {6.740516803, 3.362920996}, {6.73891736, 
3.362921781}, {6.73764641, 3.362921834}, {6.976464727, 
3.354189316}, {6.972851195, 2.955448686}, {6.960584373, 
2.944559515}, {6.941451688, 2.856659177}, {6.916453466, 
2.872523801}, {6.888799322, 2.83725635}, {6.859788043, 
3.462034734}, {6.829831408, 3.067748609}, {6.800294453, 
3.291632175}, {6.771101337, 3.457157338}, {6.740897032, 
3.213113902}, {6.707725781, 3.515985268}, {6.681497064, 
3.173971992}, {6.665355376, 3.160608058}, {6.65468603, 
3.162314592}, {6.647604098, 3.161899782}, {6.643547369, 
3.161854192}, {6.640893501, 3.161851331}, {6.968326079, 
3.092400997}, {6.964612152, 2.758367581}, {6.952001524, 
2.779234508}, {6.9322402, 2.664730667}, {6.906213587, 
2.671555346}, {6.877184042, 2.65580265}, {6.846503142, 
2.693272953}, {6.814180361, 3.12673241}, {6.781763158, 
3.277419627}, {6.748761911, 3.325077833}, {6.713427999, 
3.165498903}, {6.672982025, 3.617645559}, {6.640172368, 
3.073737955}, {6.61895858, 3.060384346}, {6.603999622, 
3.059159648}, {6.592635503, 3.052986431}, {6.584381499, 
3.056034427}, {6.578769034, 3.056624963}, {6.962765024, 
2.859040582}, {6.958993815, 2.645943758}, {6.946128126, 
2.686453784}, {6.925882664, 2.564846554}, {6.899303274, 
2.59155271}, {6.869369267, 2.575220391}, {6.837434536, 
2.598298152}, {6.803458093, 3.159433557}, {6.769012522, 
3.133085967}, {6.733400968, 3.207552825}, {6.694528099, 
3.167679581}, {6.648736976, 3.639779884}, {6.610545781, 
3.012325574}, {6.585669918, 3.369481906}, {6.567311158, 
2.987407431}, {6.553357577, 2.979041898}, {6.541958516, 
2.98001569}, {6.533118606, 2.977127321}, {6.958681796, 
2.685039585}, {6.954859829, 2.58113644}, {6.941793162, 
2.641290454}, {6.92124671, 2.510725273}, {6.894135677, 
2.56837116}, {6.86351419, 2.539615257}, {6.830740051, 
2.57673396}, {6.79550934, 3.018180298}, {6.759495118, 
3.029310982}, {6.721877721, 3.130597252}, {6.680281944, 
3.066498713}, {6.630124678, 3.306008097}, {6.587790205, 
2.903931905}, {6.559954274, 3.178817182}, {6.53923098, 
2.866788717}, {6.522652585, 2.865335777}, {6.509287055, 
2.864630931}, {6.498349444, 2.862792046}, {6.955585336, 
2.593915033}, {6.95174978, 2.582057423}, {6.93853889, 
2.730687172}, {6.917790428, 2.520559153}, {6.890254007, 
2.56596699}, {6.859138592, 2.988364424}, {6.825643863, 
2.599462725}, {6.789483734, 3.079088874}, {6.752400691, 
2.752097758}, {6.713179097, 3.32088671}, {6.669315007, 
2.443270308}, {6.615825438, 2.806002432}, {6.570298127, 
2.294253166}, {6.539806323, 2.443447679}, {6.516895962, 
2.266062601}, {6.498533237, 2.690492488}, {6.483227365, 
2.265815138}, {6.470474898, 2.269815209}, {6.953178801, 
2.498106873}, {6.949285214, 2.516910941}, {6.93598529, 
2.683738574}, {6.915053741, 2.858631419}, {6.887239263, 
2.509337756}, {6.855741275, 2.903255918}, {6.821623854, 
2.552429882}, {6.784845617, 3.003888991}, {6.746764429, 
2.685175038}, {6.706291623, 3.179205594}, {6.660628221, 
2.327311259}, {6.60472103, 2.590040214}, {6.556327746, 
2.104446758}, {6.523727251, 2.189169716}, {6.499091783, 
2.057764678}, {6.47911554, 2.19327725}, {6.462279634, 
2.043622143}, {6.447935747, 3.213002481}, {6.951259341, 
2.481056969}, {6.947350991, 2.516970762}, {6.933950342, 
2.608136516}, {6.912852778, 2.817791209}, {6.884822947, 
2.515328488}, {6.853015076, 2.860983232}, {6.818524707, 
2.56694289}, {6.781112069, 2.967621489}, {6.742290315, 
2.726788293}, {6.700939985, 2.6594261}, {6.653852707, 
2.12231287}, {6.595713829, 2.443114105}, {6.544929444, 
1.870272903}, {6.510609749, 1.950211337}, {6.484530162, 
1.816173267}, {6.463227703, 1.910042526}, {6.445058198, 
1.798471589}, {6.429532184, 1.936866545}, {6.949732547, 
2.425818733}, {6.945798062, 2.647872884}, {6.932376272, 
2.813944879}, {6.911149817, 2.661534012}, {6.882908577, 
2.762892226}, {6.850826742, 2.69687097}, {6.816023411, 
2.671563593}, {6.778192368, 2.790164386}, {6.738669474, 
2.761900904}, {6.696527895, 2.559816741}, {6.648353304, 
2.878873278}, {6.588525125, 2.154850826}, {6.535861648, 
1.629850271}, {6.500140174, 1.707699189}, {6.472835011, 
1.556257492}, {6.450336086, 1.610651685}, {6.43113596, 
1.530743306}, {6.414550213, 1.598507617}, {6.948483336, 
2.41512146}, {6.944577711, 2.672779495}, {6.931058541, 
2.807931751}, {6.909720324, 2.696789596}, {6.881332699, 
2.772042308}, {6.849049771, 2.759730298}, {6.813964267, 
2.607154141}, {6.775752408, 2.91460007}, {6.735821461, 
2.776517102}, {6.693007547, 2.474779395}, {6.643919249, 
3.061882146}, {6.582312632, 1.828557981}, {6.52842868, 
1.318150806}, {6.491511525, 1.46145624}, {6.46309646, 
1.168606532}, {6.439606376, 1.243184131}, {6.41968226, 
1.105699476}, {6.402124113, 1.161187379}, {6.947464035, 
2.407289049}, {6.943514178, 2.484775976}, {6.929981953, 
2.685880014}, {6.908572716, 2.767078431}, {6.880065636, 
2.844733616}, {6.847637996, 2.866394864}, {6.812345227, 
2.558133726}, {6.77383994, 2.964282557}, {6.733409678, 
2.852804017}, {6.690050977, 2.401487661}, {6.640309135, 
3.177482755}, {6.577518339, 1.706039542}, {6.522368289, 
3.513245993}, {6.484454258, 1.166829801}, {6.455123055, 
0.894187456}, {6.430959859, 1.048593245}, {6.410139145, 
0.771835548}, {6.391867922, 0.842580556}, {6.946644327, 
2.394509865}, {6.942665889, 2.597720865}, {6.929076585, 
2.643707164}, {6.907644583, 2.591481111}, {6.879023116, 
2.793249539}, {6.846438269, 2.575657911}, {6.810953093, 
2.819561121}, {6.772199415, 2.558582859}, {6.731529537, 
2.813200376}, {6.687766819, 2.528568343}, {6.637297718, 
3.257931369}, {6.573540851, 1.622223537}, {6.517353699, 
3.275267407}, {6.478605976, 1.070037293}, {6.448562749, 
2.799185077}, {6.423782982, 0.794084741}, {6.402317371, 
0.599477411}, {6.383344134, 0.734462636}, {6.945928126, 
2.405343228}, {6.941975511, 2.47826747}, {6.928360481, 
2.665457982}, {6.906830577, 2.803411106}, {6.878134187, 
2.774331285}, {6.845468321, 2.617861868}, {6.809857123, 
2.686404041}, {6.770833356, 2.478964041}, {6.730039181, 
2.725768607}, {6.685847795, 2.305261446}, {6.634854335, 
2.923746224}, {6.570388073, 1.546405144}, {6.513219843, 
2.61325003}, {6.473760638, 0.948137439}, {6.443185493, 
1.995912996}, {6.417729533, 0.596728631}, {6.395706918, 
1.976153715}, {6.376280034, 0.334535519}, {6.94534765, 
2.392308781}, {6.941393529, 2.477487831}, {6.927735761, 
2.671098121}, {6.906201481, 2.669899429}, {6.877462342, 
2.836139305}, {6.844631395, 2.511605154}, {6.808860414, 
2.835869359}, {6.769732478, 2.419971558}, {6.728702049, 
2.710203756}, {6.684097748, 2.260339238}, {6.632641469, 
2.838537027}, {6.567704107, 1.476304786}, {6.509759459, 
2.39030416}, {6.469765684, 0.882194919}, {6.438657748, 
1.494315494}, {6.412729015, 0.492519209}, {6.390337529, 
1.30024496}, {6.370388325, 0.177167498}, {6.9448677, 
2.388479281}, {6.940905607, 2.530093893}, {6.927250447, 
2.554735541}, {6.905617196, 2.467264542}, {6.876843774, 
2.679963867}, {6.843960337, 2.393308753}, {6.808117903, 
2.574092056}, {6.768842931, 2.371032047}, {6.727473167, 
2.838215747}, {6.682770336, 2.223367174}, {6.631018912, 
2.835450039}, {6.565479648, 1.419552194}, {6.506871279, 
1.926955165}, {6.46635346, 0.833560503}, {6.434938146, 
1.283391272}, {6.4086394, 0.423734393}, {6.385775385, 
0.925330872}, {6.365540926, 0.069801878}, {6.944457784, 
2.383420748}, {6.940474486, 2.336793071}, {6.926811707, 
2.420448091}, {6.905164352, 2.330982551}, {6.876319134, 
2.526546277}, {6.843440116, 2.326598993}, {6.807441403, 
2.89697162}, {6.768073313, 2.342206415}, {6.726614565, 
2.62499806}, {6.681711802, 2.188854817}, {6.629657497, 
2.681773662}, {6.504470027, 1.806784862}, {6.463621864, 
0.794606643}, {6.431730104, 1.09406179}, {6.405195745, 
0.370542987}, {6.381998443, 
0.659540632}, {6.361366672, -0.006277668}, {6.944114718, 
2.383243601}, {6.940127989, 2.318491247}, {6.926429223, 
2.408388294}, {6.904803241, 2.31285621}, {6.875861967, 
2.524762582}, {6.842892749, 2.312357128}, {6.806905814, 
2.639766472}, {6.767447262, 2.332406084}, {6.72580953, 
2.850519321}, {6.680802637, 2.158377068}};

bottomfitline = Table[{x, 10.6008*Sin[x] + 16.2661*Cos[x] - 16.9753 - .08}, {x, 6.3,
 7, .1}];
ListPlot[{datatofit, bottomfitline}, Joined -> {False, True}, 
 Axes -> True, PlotRange -> All, 
 AxesLabel -> (Style[#, 10] &) /@ {"x", "y"}]
$\endgroup$

2 Answers 2

5
$\begingroup$

You could try constructing a fit manually by producing your own cost function. Here I use the square difference residuals if the points error to the upside of the curve, just like least squares. But if the points fall below it I make the costs harsher by using an exponentially rising cost and penalty multiplier. Here $\varepsilon$ is the absolute residual, the $x_i,y_i$ are the points, $p$ is the penalty and $f$ is the model to fit.

$$ \varepsilon=\left|y_i-f(x_i)\right|\\\text{cost}(x_i,y_i|f)= \begin{cases} \varepsilon^2 & y_i\geq f(x_i) \\ \exp(p \varepsilon) & \text{otherwise} \end{cases} $$

We then minimize the total costs. A higher penalty leads to a flatter line which is less tolerant of any points falling below the curve, while a lower penalty creates a more curvy line that allows a few points to fall below:

residual[f_, data_, penalty_] := Sum[
  With[{fy = f[p[[1]]], y = p[[2]], x = p[[1]]},
   Piecewise[{{(fy - y)^2, y >= fy}}, Exp[penalty*(fy - y)]]
   ],
  {p, data}, Method -> "Procedural"]

f[x_] := a*Sin[x] + b*Cos[x] - c

{err, sol} = NMinimize[residual[f, datatofit, 15.], {a, b, c}, 
   Method -> "SimulatedAnnealing"];

fittedmodel = f[x] /. sol

Show[
 ListPlot[datatofit, PlotRange -> All, 
  AxesLabel -> (Style[#, 10] &) /@ {"x", "y"}],
 Plot[fittedmodel, {x, Min[datatofit[[All, 1]]], 
   Max[datatofit[[All, 1]]]}]
 ]

enter image description here

$\endgroup$
5
  • $\begingroup$ Thank you very much this was very helpful $\endgroup$ Jan 6 at 22:54
  • $\begingroup$ I'm now trying to change this to an upper bound. It isn't working well currently. Do you have any suggestions? I've modified the code by changing the residual function to this: residual[f_, data_, penalty_] := Sum[With[{fy = f[p[[1]]], y = p[[2]], x = p[[1]]}, Piecewise[{{(y - fy)^2, y <= fy}}, Exp[penalty*(y - fy)]]], {p, data}, Method -> "Procedural"] $\endgroup$ Jan 9 at 15:52
  • $\begingroup$ @mathemagician617 in what way is it not working well? I took your changes and they produce an upper bound which is mostly okay on the right but too conservative on the initial left slope. If the fit is the problem, you should probably look for a better model for the upper bound, and your data are heteroskedastic which is a common cause of problems in regressions. $\endgroup$
    – flinty
    Jan 10 at 10:33
  • $\begingroup$ @flinty Where did you find Method -> "Procedural" ? $\endgroup$ Jan 11 at 7:59
  • $\begingroup$ @flinty thank you $\endgroup$ Jan 12 at 16:31
4
$\begingroup$

Because you have no theoretical model to represent the "bottom bound of the data", that feature is in the eye of the beholder. It seems that there are two tasks: (1) Find a set of data points that represent the bottom bound of the data, and (2) Attempt to find a curve that adequately describes that set of bounding data.

While you could just draw in a bottom bound that satisfies the definition in your mind, the excellent link you gave (https://community.wolfram.com/groups/-/m/t/607510 with answer by Marco Theil can get you task 1.

(* Interpolate the raw data so that you have an approximation of that data on a fine and regular grid *)
f = Interpolation[data, InterpolationOrder -> 1];
t = Table[{x, f[x]}, {x, 6.35, 7.05, 0.0001}];

(* Estimate the "bottom" of the curve based on the excellent post you mentioned *)
σ0 = 50;  (* You need to decide on this value *)
base = EstimatedBackground[TimeSeries[t], σ0, Method -> {"SNIP", 0}];
ListLinePlot[{data, base}]

raw data and estimate of bottom bound of data

Now you need to determine a curve form that adequately describes the bottom bound. Two linear segments with sines and cosines seems promising.

$\endgroup$
3
  • $\begingroup$ Thank you for your response $\endgroup$ Jan 6 at 23:01
  • 1
    $\begingroup$ If your objective was more about finding smoother boundary (like what @flinty did) but you wanted the boundary where say only 5% of the points would be below (as opposed to 0% as stated in your question, then you might consider quantile regression. $\endgroup$
    – JimB
    Jan 7 at 1:22
  • $\begingroup$ @JimBanks for the suggestion! $\endgroup$ Jan 8 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.