[R] Fitting an Inverse Gamma Distribution

David Scott d.scott at auckland.ac.nz
Fri Jan 14 00:35:15 CET 2011


On 14/01/2011 11:46 a.m., emorway wrote:
>
> http://r.789695.n4.nabble.com/file/n3216865/Inverse_Gamma.png
>
> Hello,
>
> I am seeking help in estimating the parameters of an inverse gamma
> distribution (from the 'actuar' package) using a function like 'fitdistr'.
> Unfortunately I haven't found such a package using findFn('fit Inverse
> Gamma') from the 'sos' package and was therefore hoping someone might be
> aware of such a function?

In my package GeneralizedHyperbolic on R-Forge (not yet in the CRAN 
version) you will find a fitting routine for the generalized inverse 
Gaussian distribution which has the inverse Gaussian as a special case.

David Scott

>
> Secondly, is there a way to shift the pdf (code below) to the right (rather
> than the data to the left)?  I tried:
>
> par(new=T)
> shift<-1
> hist(iniSal_US_forHist,breaks=seq(1.1,21,by=0.625),col="grey",freq=F,xlim=c(0-shift,21-shift),plot=F)
> curve(dinvgamma(x,scale=11.835,shape=4.4242),from=0,to=20,add=T,col="purple",lwd=2)
>
> but this failed in shifting the curve.  More broadly, the data plotted in
> the histogram represents a calibration target for output generated by a
> finite difference solute transport model.  The values that will be generated
> by the finite difference model would ideally fit the shifted (if that can be
> figured out) inverse gamma pdf.  To the extent that fit is deemed poor, the
> parameter estimation software associated with the finite difference model
> will adjust parameters until a better fit is found.  I will try to use a
> goodness of fit test to determine if one set of parameter values in the
> finite difference model produce output that fits the inv. gamma curve herein
> better than another.  But first I need to establish the curve the finite
> difference model output should target and would greatly appreciate any leads
> on how it might be shifted and/or fit more precisely by a fitdistr-like
> function.
>
> library(MASS)
> library(actuar)
>
> iniSal_US_forHist<-c(2.368000,3.532614,3.064330,3.347069,3.066333,4.233636,3.465650,2.858553,
> 2.946731,2.945417,2.415000,2.873019,5.521000,5.788148,5.314630,5.509672,6.032840,6.009310,
> 4.110833,6.073182,5.652833,4.425733,6.481852,4.076857,3.289310,4.524000,3.985811,5.399714,
> 4.490606,6.956729,5.270933,8.099107,5.058250,6.394500,5.644000,5.202459,5.666667,3.152680,
> 3.220952,2.777381,3.115467,3.642759,3.488333,3.022439,2.610290,2.618571,3.218000,3.417634,
> 10.327317,7.344270,6.886154,4.015800,3.063103,6.832292,4.600238,2.939000,5.999027,7.894878,
> 4.411538,2.384762,6.816154,2.782500,2.475333,2.799138,2.739063,2.619917,2.892545,2.468167,
> 2.577079,2.821875,2.502500,2.969032,2.046023,3.073077,4.408000,3.411774,3.500000,4.283607,
> 4.284000,4.276714,3.228103,2.639875,3.453194,2.821200,3.838723,1.714253,2.273750,2.611882,
> 2.321781,2.567500,2.557045,1.288875,2.175211,1.736000,2.250781,7.433366,7.033553,5.474444,
> 7.132727,8.505937,9.174545,6.554487,7.060286,6.617160,8.210986,4.404045,6.062381,5.149625,
> 2.972105,5.358889,3.910968,3.715873,1.728966,2.843667,4.413906,3.016346,7.168636,3.839394,
> 3.930141,7.019882,3.459429,5.050250,3.492714,3.226667,3.987667,2.770227,3.661167,1.553000,
> 2.867391,2.897193,2.611707,2.577167,2.904697,2.733077,2.507241,11.044865,6.425484,8.567222,
> 8.552344,7.493396,4.807381,9.697869,9.471333,6.783175,4.563571,8.059649,9.448679,5.803778,
> 4.769423,4.424634,7.586042,4.451556,3.622373,6.390152,4.424375,4.135806,5.025400,5.410635,
> 7.012292,2.961071,3.192188,2.989643,3.471429,2.867966,1.980541,3.172344,2.574783,2.958983,
> 1.708140,3.604853,3.479000,2.845000,2.742603,2.923968,3.620308,2.452500,2.721375,3.166333,
> 2.742162,2.793000,3.337000,5.192025,5.365875,3.079000,8.415970,6.612277,6.734706,4.856857,
> 5.164783,7.743667,6.894151,4.666538,9.227167,8.077581,6.109833,6.621724,18.098182,12.705600,
> 15.490784,17.394750,12.422364,14.832727,8.326000,11.352400,3.431429,2.658261,3.219773,3.605185,
> 4.030299,3.262241,3.503250,3.522763,2.847312,2.996618,3.075769,3.387731,3.066923,3.078200,
> 2.466957,3.214167,2.707778,3.384839,2.283556,2.912258,3.378000,2.726750,2.950000,2.195000,
> 4.819063,3.604578,3.694906,5.068000,4.676582,3.028831,4.261042,3.593235,4.501224,2.880317,
> 5.750333,3.257833,3.967458,2.522292,2.725738,2.549231,2.591389,2.990488,2.681222,2.685854,
> 2.284750,2.585938,2.432824,3.108875,2.611340,3.916667,2.418095,2.476406,2.801235,3.278000,
> 2.434921,2.617826,3.133939,2.774321,4.196173,3.764286,3.555833,5.317361,3.970800,4.136400,
> 4.487013,3.746393,4.754000,3.854316,3.742353,3.044079,2.817821,3.995179,3.643134,3.642593,
> 3.604533,2.935902,4.088310,5.344407,3.076883,3.287105,3.720870,2.032258,2.872593,5.787313,
> 6.017838,5.425205,4.880600,3.582295,4.903333,3.489016,4.603030,5.344407,6.184286,4.047083,
> 4.788304,4.661325,4.815938,4.056790,3.765595,5.348772,5.200222,4.906311,3.900147,3.782897,
> 3.767313,3.417732,3.725455,2.888750,2.552333,2.521613,2.531522,2.510833,2.710208,2.445273,
> 2.619750,2.094737,2.399355,2.758000,2.317077,2.247755,3.594333,4.607805,2.693333,3.084706,
> 3.529000,2.326200,3.309851,2.647805,2.802250,2.778667,3.231235,2.418065,3.134545,3.807843,
> 2.988372,2.709792,3.084035,3.633279,2.958750,2.170081,2.674444,2.640706,2.841600,3.399219,
> 2.561373,2.574824,3.046447,2.647500,3.554875,1.865000,2.858333,2.355000,2.508082,2.376833,
> 2.728710,1.752833,1.571967,1.800333,2.265455,2.353226,2.568028,2.359500,2.472639,1.675385,
> 2.667386,2.490000,2.482632,2.593452,2.695510,2.466941,2.624211,3.835645,3.519667,2.661940,
> 3.516167,3.146585,3.420462,4.809833,3.028500,3.192297,4.256333,2.516897,3.033846,2.793359,
> 6.700714,5.441250,6.872500,4.528333,7.490328,4.673788,6.376885,3.023167,4.429167,4.317625,
> 16.729231,8.372500,6.279828,10.805098,8.359452,8.277576,8.008846,8.742308,12.155942,5.975063,
> 3.317333,2.883021,3.310822,3.119219,3.921190,3.552986,3.647162,4.017667,3.895342,3.381096,
> 2.769412,3.225294,4.169333,3.733919,2.859492,3.674875,3.401017,3.160267,4.109545,4.347867,
> 3.867000,3.672763,4.364844,3.804250,2.613000,4.289909,4.212059,4.785797,4.149687,6.299444,
> 5.640135,5.713448,4.766438,7.032027,5.610533,3.126111,6.322029,4.417692,6.392532,2.753175,
> 2.549655,3.456533,3.199000,3.852338,3.387549,3.098033,3.271733,3.679180,2.655484,6.858167,
> 5.808033,7.551111,8.388387,5.108732,7.895152,5.223580,5.741714,8.026250,4.928421,2.797292,
> 3.052500,2.934615,2.842051,2.869259,2.702400,2.666977,2.935385,2.546471,2.617755,3.020250,
> 2.922955,3.133500,3.353750,3.101000,2.581235,3.624715,2.376164,3.597467,3.011519,2.885250,
> 2.642716,2.843788,3.125000,2.394034,3.126125,2.960125,3.553800,3.045733,2.823375,2.953951,
> 3.170694,3.746500,3.421519,2.876707,3.162206,2.976852,2.912917,3.510784,2.822763,3.245244,
> 3.322738,3.168776,2.822700,2.898500,4.311667,3.517195,4.239014,3.615676,3.625652,4.693542,
> 3.426379,3.446250,4.204375,4.061000,2.864167,3.498772,3.817303,2.649300,2.913529,2.773400,
> 3.844833,3.312581,4.072025,3.578395,3.856429,3.666889,2.554505,2.615469,2.686667,4.848387,
> 3.182667,3.172364,2.829063,2.998182,2.224000,3.238167,4.072407,5.915490,5.872632,4.942597,
> 6.163684,5.003750,1.592295,2.398679,2.360000,1.930200,2.341746,2.352031,2.577647,2.286604,
> 2.172000,2.430889,2.647231,2.748500,2.837692,2.764884,2.511774,3.047451,2.744737,2.733871,
> 2.666818,3.047800,6.127000,3.212807,7.100615,5.016842,5.404091,4.586324,4.815738,2.667867,
> 2.583625,3.023099,2.364154,3.033077,2.712821,3.104861,2.451831,10.244054,9.028333,6.488088,
> 11.350333,10.805227,2.796250,3.901455,8.846500,5.692500,4.502549,9.645833,12.323077,11.690702,
> 9.757213,10.140000,6.435873,8.559375,12.581639,9.658000,7.910395,10.317167,14.744098,3.019400,
> 3.196154,2.924583,2.926393,3.435926,2.940968,2.729048,2.419625,3.257031,2.246915,2.405352,
> 4.807167,3.329661,3.300256,2.626364,2.588714,2.516825,3.369804,2.802381,2.703889,1.899500,
> 2.624286,3.370244,3.224510,2.587368,3.243881,3.209474,2.644154,3.591132,3.179851,3.118125,
> 3.428333,3.078033,2.639219,2.392979,3.035208,8.924667,7.126420,4.412626,7.663580,9.798000,
> 7.091940,6.561806,10.351000,7.816000,5.686581,7.486833,5.878148,5.549032,1.775849,3.393235,
> 3.730208,3.131846,2.988404,3.922286,3.835077,1.918780,2.614118,2.564207,3.160625,2.085614,
> 2.498033,1.107183,2.408000,2.226296,3.063281,2.647500,3.149726,1.929091,2.297000,4.035500,
> 7.813846,18.976765,10.127547,12.294455,5.909020,7.263115,13.789367,6.608750,3.110926,4.948333,
> 7.655873,5.366154,2.468889,3.215000,3.329375,2.722821,3.115902,2.538235,3.233171,3.060541,
> 3.377647,2.837049,3.071290,2.293400,3.034937,2.943846,2.987534,2.947222,3.286522,2.794600,
> 2.950822,1.542667,2.318085,3.049583,2.384250,2.554146,1.761333,2.028356,2.434590,3.128333,
> 2.838437,3.331000,2.645116,3.038667,2.646000,3.082857,4.406301,4.136000,2.161429,1.832424,
> 3.027742,3.908958,3.479608,3.566562,2.101875,2.960122,2.804752,2.334105,3.993836,2.878116,
> 2.828197,2.674353,3.231500,2.468000,2.152400,3.290274,5.412121,2.775909,9.023833,6.593125,
> 3.417558,4.213469,2.771452,7.874776,2.453871,3.166833,2.657000,2.526111,3.105400,2.973265,
> 4.900667,3.042551,2.861647,3.745125,3.244643,2.177500,2.119167,2.798000,3.005577,2.837347,
> 3.211167,3.381358,3.200306,4.640167,5.564706,3.043121,6.172000,2.490000,4.449667,5.433918,
> 4.652576,3.197619,2.675631,2.547407,3.462195,2.657468,3.016087,2.814262,2.966383,3.288033,
> 2.278676,2.609537,2.633667,2.815000,3.465000,2.231538,3.161538,3.412667,3.516800,3.249452,
> 2.418734,3.245455,2.650800,3.634038,3.255484,1.992093,3.184429,3.155200,2.997500,3.887931,
> 2.597500,3.316406,3.684091,3.707107,4.479692,3.292833,2.806935,2.833143,2.962857,2.229474,
> 3.786700,4.029756,3.174118,6.493770,4.611944,6.103279,5.899687,2.946711,6.338660,5.237273,
> 3.413553,3.226667,4.193143,6.192375,9.437436,1.863548,3.185567)
>
> #plot it
> hist(iniSal_US_forHist,breaks=seq(1.1,21,by=0.625),col="grey",freq=F,xlim=c(0,21))
>
> #try fitting it
> curve(dinvgamma(x,scale=11.835,shape=4.4242),from=0,to=20,add=T,col="blue",lwd=2)
>


-- 
_________________________________________________________________
David Scott	Department of Statistics
		The University of Auckland, PB 92019
		Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email:	d.scott at auckland.ac.nz,  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics



More information about the R-help mailing list