我正在尝试将指数函数拟合到R

在R中,我有一个大型数据框,其中包含1000个具有指数分布的模拟。

当我使用gg_plot时,得到的图形如下:

我正在尝试将指数函数拟合到R

我正在尝试估计该图的指数函数的值,然后使用这些值绘制一条线。

我对堆栈溢出很陌生。回到图表,我首先将y和x值(分别为q和t)转换为对数形式,并进行了线性回归。看起来像这样:

surscript$logq<-log(surscript$q)
surscript$logt<-log(surscript$t)
linearmod<-lm(surscript$logq~surscript$logt)

那使我的y截距为-14.273,增长率为1.717。 为了将其转换回指数,我对这些值执行了指数函数。

expmodint<-exp(-14.273)
expmodgrowth<-exp(1.717)

然后我使用指数方程y = a * e ^ t来描述q值:

temp<-expmodint*(expmodgrowth^surscript$t)

然后我创建了一个数据框,其中包含此指数方程式和原始数据框的t:

 temp1<-data.frame(temp,surscript$t)

然后我尝试使用geom_line将这条线添加到现有图形中,如上所示:

p+geom_line(temp1,aes(x=temp,y=surscript$t))

但是我返回错误“映射必须由“ aes”创建”。

有人可以建议为什么会发生此错误,以及可能是一种更好的计算和拟合指数曲线的方法吗?

编辑:在这里,我包含了两个模拟的数据框,因此,您将能够处理一些数据。

structure(list(sim = structure(c(1L,1L,2L,2L),.Label = c("simulation 1","simulation 2"),class = "factor"),m = c(0L,0L,1L),q = c(0.001,0.001,0.002,0.005,0.006,0.007,0.011,0.016,0.02,0.026,0.033,0.035,0.041,0.045,0.05,0.052,0.054,0.058,0.064,0.066,0.071,0.075,0.08,0.087,0.097,0.111,0.124,0.132,0.143,0.154,0.162,0.176,0.188,0.205,0.216,0.225,0.231,0.241,0.252,0.269,0.287,0.301,0.32,0.345,0.358,0.383,0.406,0.429,0.454,0.479,0.493,0.521,0.541,0.565,0.587,0.606,0.635,0.654,0.677,0.694,0.712,0.729,0.744,0.766,0.783,0.805,0.815,0.824,0.835,0.849,0.859,0.866,0.881,0.888,0.9,0.912,0.92,0.925,0.934,0.941,0.944,0.949,0.954,0.959,0.963,0.965,0.97,0.974,0.977,0.978,0.98,0.981,0.984,0.986,0.987,0.988,0.989,0.99,0.991,0.992,0.993,0.995,0.996,0.997,0.998,0.999,1,0.003,0.022,0.028,0.031,0.036,0.043,0.044,0.051,0.056,0.059,0.072,0.076,0.083,0.088,0.096,0.099,0.103,0.118,0.125,0.129,0.139,0.145,0.148,0.156,0.166,0.175,0.184,0.191,0.197,0.2,0.207,0.211,0.219,0.224,0.238,0.25,0.265,0.273,0.289,0.3,0.313,0.328,0.344,0.354,0.368,0.378,0.387,0.399,0.411,0.424,0.439,0.455,0.468,0.48,0.497,0.503,0.513,0.52,0.53,0.539,0.552,0.56,0.569,0.581,0.588,0.596,0.604,0.618,0.626,0.64,0.672,0.688,0.705,0.723,0.733,0.747,0.765,0.781,0.795,0.807,0.818,0.825,0.836,0.843,0.854,0.863,0.872,0.875,0.883,0.889,0.901,0.906,0.914,0.928,0.936,0.943,0.953,0.958,0.96,0.967,0.968,0.973,0.975,0.979,0.982,0.994,1),t = c(21,51,81,111,141,171,201,231,261,291,321,351,381,411,441,471,501,531,561,591,621,651,681,711,741,771,801,831,861,891,921,951,981,1011,1041,1071,1101,1131,1161,1191,1221,1251,1281,1311,1341,1371,1401,1431,1461,1491,1521,1551,1581,1611,1641,1671,1701,1731,1761,1791,1821,1851,1881,1911,1941,1971,2001,2031,2061,2091,2121,2151,2181,2211,2241,2271,2301,2331,2361,2391,2421,2451,2481,2511,2541,2571,2601,2631,2661,2691,2721,2751,2781,2811,2841,2871,2901,2931,2961,2991,3021,3051,3081,3111,3141,3171,3201,3231,3261,3291,3321,3351,3381,3411,3441,3471,3501,3531,3561,3591,3621,3651,3681,3711,3741,3771,3801,3831,3861,3891,3921,3951,3981,4011,4041,4071,4,34,64,94,124,154,184,214,244,274,304,334,364,394,424,454,484,514,544,574,604,634,664,694,724,754,784,814,844,874,904,934,964,994,1024,1054,1084,1114,1144,1174,1204,1234,1264,1294,1324,1354,1384,1414,1444,1474,1504,1534,1564,1594,1624,1654,1684,1714,1744,1774,1804,1834,1864,1894,1924,1954,1984,2014,2044,2074,2104,2134,2164,2194,2224,2254,2284,2314,2344,2374,2404,2434,2464,2494,2524,2554,2584,2614,2644,2674,2704,2734,2764,2794,2824,2854,2884,2914,2944,2974,3004,3034,3064,3094,3124,3154,3184,3214,3244,3274,3304,3334,3364,3394,3424,3454,3484,3514,3544,3574,3604,3634,3664,3694,3724,3754,3784,3814,3844,3874,3904,3934,3964,3994,4024,4054,4084,4114,4144,4174,4204,4234,4264,4294,4324,4354,4384,4414,4444,4474,4504,4534,4564),n = c(1,logq = c(-6.90775527898214,-6.90775527898214,-6.21460809842219,-5.29831736654804,-5.11599580975408,-4.96184512992682,-4.50986000618377,-4.13516655674236,-3.91202300542815,-3.64965874096066,-3.41124771751566,-3.35240721749272,-3.19418321227783,-3.10109278921182,-2.99573227355399,-2.95651156040071,-2.91877123241786,-2.84731226843572,-2.74887219562247,-2.71810053695571,-2.64507540194082,-2.59026716544583,-2.52572864430826,-2.44184716032755,-2.33304430047875,-2.1982250776698,-2.0874737133771,-2.02495335639577,-1.94491064872223,-1.87080267656851,-1.82015894374975,-1.73727128394399,-1.67131331615219,-1.58474529984373,-1.53247687129797,-1.49165487677772,-1.46533756846034,-1.42295834549148,-1.37832619147071,-1.3130438993803,-1.24827306322252,-1.20064501423326,-1.13943428318836,-1.06421086195078,-1.02722229258144,-0.959720289801491,-0.901402119380404,-0.84629836005412,-0.789658080940789,-0.736054681571222,-0.707246104939447,-0.65200523722877,-0.614336000135655,-0.570929547835696,-0.532730459154041,-0.500875292912823,-0.454130280089445,-0.424647927524938,-0.390084006069862,-0.365283318475333,-0.339677367570161,-0.316081546973479,-0.295714244149045,-0.266573109241546,-0.244622582991334,-0.216913001563574,-0.204567165741274,-0.193584749072665,-0.180323554131282,-0.16369609267079,-0.151986356997882,-0.143870370419702,-0.126697653045958,-0.118783535989967,-0.105360515657826,-0.0921152889078056,-0.083381608939051,-0.0779615414697118,-0.0682788407532944,-0.0608121393967575,-0.0576291128366364,-0.0523464803722092,-0.0470916075338506,-0.0418642040986989,-0.0377018671840115,-0.0356271776431512,-0.0304592074847086,-0.026343975339602,-0.0232686269393543,-0.0222456089473197,-0.0202027073175195,-0.019182819416774,-0.0161293819298836,-0.0140989243795016,-0.0130852395486555,-0.0120725812342692,-0.0110609473594249,-0.0100503358535015,-0.00904074465214907,-0.00803217169726427,-0.00702461493696447,-0.00501254182354429,-0.00400802139753882,-0.00300450902029872,-0.00200200267067308,-0.00100050033358353,-5.80914299031403,-3.81671282562382,-3.57555076880693,-3.47376807449699,-3.32423634052603,-3.14655516328857,-3.12356564506388,-2.97592964625781,-2.88240358824699,-2.83021783507642,-2.63108915996608,-2.57702193869581,-2.48891467118554,-2.43041846450393,-2.3434070875143,-2.31263542884755,-2.2730262907525,-2.13707065451647,-2.07944154167984,-2.04794287462046,-1.97328134585145,-1.93102153656156,-1.91054300521802,-1.8578992717326,-1.79576749062559,-1.74296930505862,-1.69281952137315,-1.65548185093551,-1.62455155024415,-1.6094379124341,-1.57503648571677,-1.55589714550607,-1.51868354916564,-1.4961092271271,-1.43548460531066,-1.38629436111989,-1.32802545299591,-1.29828348379718,-1.2413285908697,-1.20397280432594,-1.16155208844198,-1.11474167059799,-1.06711362160874,-1.03845836584836,-0.999672340813206,-0.972861083362549,-0.949330585952355,-0.918793862092274,-0.889162064485903,-0.858021823750179,-0.823255865906966,-0.787457860031187,-0.75928698306449,-0.7339691750802,-0.699165252885508,-0.687165108882398,-0.667479433811368,-0.653926467406664,-0.63487827243597,-0.61803970807314,-0.594207232705042,-0.579818495252942,-0.563874844855806,-0.543004522130226,-0.53102833108351,-0.517514611916787,-0.504181081047322,-0.481266821524446,-0.468404907882039,-0.446287102628419,-0.397496938458987,-0.373966441048793,-0.349557476169868,-0.324346056823372,-0.310609577095486,-0.29169009384932,-0.267879445155601,-0.247180129142451,-0.229413164327805,-0.214431610712188,-0.20089294237939,-0.192371892647456,-0.179126665897435,-0.170788320980282,-0.157824085193567,-0.147340587898709,-0.136965855073157,-0.133531392624523,-0.124430078378177,-0.117658043468232,-0.104250021373799,-0.0987159729391577,-0.089924707527987,-0.0747235461959364,-0.0661398025045449,-0.0586889963486796,-0.048140375327935,-0.0429075010112765,-0.0408219945202552,-0.0335567835288428,-0.0325231917055601,-0.027371196796132,-0.0253178079842899,-0.0212236364516267,-0.0181639706276712,-0.00601807232556302,0),logt = c(3.04452243772342,3.93182563272433,4.39444915467244,4.70953020131233,4.94875989037817,5.14166355650266,5.30330490805908,5.44241771052179,5.56452040732269,5.67332326717149,5.77144112313002,5.86078622346587,5.9427993751267,6.01859321449623,6.08904487544685,6.15485809401642,6.21660610108486,6.27476202124194,6.3297209055227,6.3818160174061,6.43133108193348,6.47850964220857,6.52356230614951,6.56667242980324,6.60800062529609,6.64768837356333,6.68586094706836,6.72262979485545,6.75809450442773,6.79234442747081,6.82546003625531,6.85751406254539,6.88857245956536,6.91869521902047,6.94793706861497,6.97634807044775,7.00397413672268,7.03085747611612,7.05703698169789,7.0825485693553,7.1074254741107,7.13169851046691,7.15539630189673,7.1785454837637,7.20117088328168,7.22329567956231,7.24494154633701,7.26612877955645,7.2868764117507,7.30720231476474,7.32712329225929,7.34665516317654,7.36581283720947,7.38461038317697,7.40306109109009,7.42117752859539,7.43897159239586,7.45645455517621,7.47363710849621,7.49052940206071,7.50714107972761,7.5234813125735,7.53955882930103,7.55538194424027,7.57095858316901,7.58629630715272,7.60140233458373,7.61628356158038,7.63094658089046,7.64539769942863,7.65964295456468,7.67368812926773,7.68753876620163,7.70120018085745,7.71467747380093,7.72797554210556,7.74109909003537,7.75405263903576,7.76684053708551,7.77946696745832,7.79193595693806,7.80425138352811,7.8164169836918,7.82843635915759,7.84031298332016,7.85205020726589,7.86365126544865,7.87511928104029,7.88645727097769,7.89766815072691,7.90875473878325,7.91971976092457,7.93056585423396,7.94129557090653,7.95191138185419,7.96241568012106,7.9728107841214,7.98309894071089,7.99328232810159,8.00336305862995,8.01334318138667,8.02322468471667,8.03300949859667,8.04269949689764,8.05229649953865,8.06180227453835,8.07121853996986,8.0805469658245,8.08978917578932,8.09894674894334,8.10802122137675,8.11701408773731,8.12592680270789,8.13476078241865,8.14351740579748,8.15219801586179,8.16080392095467,8.16933639592839,8.17779668327778,8.18618599422608,8.19450550976564,8.20275638165564,8.21093973337902,8.2190566610606,8.22710823434815,8.23509549725836,8.24301946898925,8.25088114470065,8.25868149626424,8.26642147298455,8.27410200229233,8.28172399041139,8.28928832300032,8.29679586577005,8.30424746507847,8.31164394850298,1.38629436111989,3.52636052461616,4.15888308335967,4.54329478227,4.82028156560504,5.03695260241363,5.21493575760899,5.36597601502185,5.4971682252932,5.61312810638807,5.71702770140622,5.8111409929767,5.89715386763674,5.97635090929793,6.04973345523196,6.11809719804135,6.18208490671663,6.24222326545517,6.29894924685594,6.35262939631957,6.40357419793482,6.45204895443723,6.49828214947643,6.5424719605068,6.58479139238572,6.62539236800796,6.66440902035041,6.70196036600254,6.73815249459596,6.77308037565554,6.80682936039218,6.83947643822884,6.87109129461055,6.90173720665657,6.93147180559945,6.96034772910131,6.98841318199959,7.01571242048723,7.04228617193974,7.06817200038804,7.09340462586877,7.11801620446533,7.1420365747068,7.16549347506085,7.18841273649695,7.21081845347222,7.23273313617761,7.25417784645652,7.27517231945277,7.29573507274928,7.31588350450979,7.3356339819272,7.35500192110526,7.37400185935016,7.39264752072162,7.41095187558364,7.42892719480227,7.44658509915773,7.46393660446893,7.48099216286952,7.49776170062257,7.51425465281641,7.53047999524554,7.54644627374602,7.56216163122565,7.57763383260273,7.59287028784482,7.60787807327851,7.6226639513236,7.63723438878947,7.6515955738576,7.6657534318617,7.67971363996637,7.69348164083518,7.70706265537047,7.72046169459972,7.7336835707759,7.74673290775362,7.7596141506969,7.77233157516961,7.7848892956551,7.79729127354747,7.80954132465341,7.82164312623998,7.8336002236611,7.84541603659248,7.85709386490249,7.86863689418417,7.88004820097158,7.89133075766189,7.90248743716286,7.91352101728389,7.92443418488756,7.93522953981691,7.94590959861313,7.95647679803678,7.96693349840484,7.97728198675515,7.98752447984877,7.9976631270201,8.00770001288403,8.01763715990848,8.02747653086048,8.03722003113301,8.04686951095958,8.05642676752298,8.06589354696427,8.07527154629746,8.0845624152353,8.09376775793108,8.10288913464087,8.11192806331074,8.12088602109284,8.12976444579417,8.13856473726163,8.14728825870662,8.15593633797239,8.16451026874704,8.17301131172497,8.18144069571937,8.18979961872823,8.19808924895612,8.20631072579402,8.21446516075919,8.22255363839696,8.23057721714645,8.23853693017177,8.24643378616036,8.25426877009018,8.26204284396694,8.26975694753298,8.277411998949,8.28500889544988,8.29254851397576,8.30003171177957,8.30745932701195,8.31483217928456,8.3221510702129,8.32941678393932,8.33663008763715,8.34379173199684,8.35090245169481,8.35796296584568,8.36497397843873,8.3719361787591,8.37885024179449,8.38571682862785,8.39253658681668,8.39931015075952,8.40603814205008,8.41272116981953,8.41935983106747,8.42595471098197
)),row.names = c(NA,289L),class = "data.frame")



iCMS 回答:我正在尝试将指数函数拟合到R

情节的定义存在两个问题。
由于组因子不适用于所有geom_line()或已重新定义,因此应将其从ggplot函数移至特定的geom_line()定义。

此外,由于您要在定义中添加新的数据框,因此必须显式地将“ data =“添加到函数中。

此外,根据您的模型:lm(surscript$logq~surscript$logt),“ temp”的等式不正确,应该为:q_predicted =exp(-14.273)*(t^1.717)。如果您确实希望y = b * a ^ t,则您的模型应为:lm(surscript$logq~surscript$t),q_predicted = exp(intercept)* exp(slope)^ t。

library(ggplot2)

expmodint<- exp(-14.273)
expmodgrowth<- (1.717)
#q=exp(-14.273)*(t^1.717)
temp<-expmodint*(surscript$t^expmodgrowth)

temp1<-data.frame(temp,surscript$t)
head(temp1)  #notice the name change

ggplot(surscript,aes(x=t,y=q)) + 
   geom_line(aes(group=sim)) +
   geom_line(data=temp1,aes(x=surscript.t,y=temp),color="blue")

enter image description here

本文链接:https://www.f2er.com/2196939.html

大家都在问