From ehlers at ucalgary.ca  Mon Jan  4 23:00:12 2010
From: ehlers at ucalgary.ca (Peter Ehlers)
Date: Mon, 04 Jan 2010 15:00:12 -0700
Subject: [Rd] log normal overlay
In-Reply-To: <1262634594476-998495.post@n4.nabble.com>
References: <1262634594476-998495.post@n4.nabble.com>
Message-ID: <4B4264EC.9080707@ucalgary.ca>



emorway wrote:
> Hello,
> 
> Using the following lines of code, I created the following graph:
> 
> d.resid<-c(-5.63,4.25,4.38,3.05,-1.85,1.40,1.80,-0.80,4.20,3.20,-2.70,-7.20,-0.10,-2.50,0.60,1.20,1.30,1.20,1.30,1.27,6.91,8.55,7.18,8.73,1.82,-1.45,5.91,5.45,-0.82,-4.55,0.82,82.91,1.73,3.09,5.64,-1.73,-9.55,4.27,17.45,9.64,-18.33,21.77,-31.56,-0.65,-13.11,-14.75,-18.75,-15.56,-3.11,-1.75,-1.84,-2.02,-8.93,-7.47,0.89,10.77,-15.93,-16.93,-13.33,-9.05,-2.25,-3.53,4.95,5.13,69.38,61.76,71.18,20.27,60.51,-5.38,-14.52,-5.04,3.02,-13.19,-14.70,-14.53,-8.81,4.30,-7.89,-6.37,60.72,84.11,25.01,91.55,95.65,96.13,92.23,93.22,91.74,92.12,90.79,85.30,73.77,72.44,65.51,49.31,45.50,-10.31,-22.50,-20.21,-3.20,-11.32,-9.29,-1.75,-0.91,-1.93,-0.23,-6.32,-0.84,-3.89,-5.04,1.71,-18.29,-36.63,-36.75,-24.63,-24.95,-27.85,-26.60,-21.20,-14.80,-29.80,-22.80,-21.70,-26.20,-25.10,-37.50,-34.40,-34.80,-33.70,-34.80,-33.70,-19.73,-18.09,-23.45,-23.82,-19.27,-20.18,-24.45,-25.09,-23.55,-25.82,-21.55,-24.18,-34.09,-27.27,-25.91,-26.36,-28.73,-23.55,-32.73,-29.55,-28.36,-30.41,-125.31,-15.64,-24.73,-19
.1!
>  9,-19.83,-17.83,-19.64,-22.19,-21.83,-22.92,-51.10,-27.01,-27.55,-26.19,-17.01,-5.63,-0.75,-5.63,-2.95,9.15,4.40,6.80,-0.80,-0.80,9.20,10.30,12.80,5.90,6.50,-0.40,-6.80,-4.70,-6.80,-4.70,7.27,7.91,3.55,13.18,1.73,3.82,6.55,6.91,16.45,9.18,5.45,11.82,-5.09,13.73,15.09,20.64,9.27,15.45,10.27,5.45,2.64,-3.41,7.69,13.36,7.27,12.81,7.17,17.17,19.36,14.81,12.17,24.08,19.90,21.99,19.45,15.81,-67.31,24.99,24.99,14.59,11.64,15.38,14.10,16.49,27.00,-58.88,-89.97,-59.18,-65.28,-62.50,-69.03,-75.57,-89.66,-58.22,-49.96,-61.24,-68.06,-41.82,-80.30,-60.49,-70.98,-79.45,-77.85,-127.19,-62.80,-103.81,-119.91,-102.65,-113.82,-105.53,19.62,17.05,15.38,12.25,35.38,36.05,33.15,30.40,38.80,35.20,17.20,32.20,59.30,61.80,15.90,35.50,28.60,10.20,14.30,10.20,14.30,33.27,22.91,10.55,4.18,10.73,31.82,32.55,13.91,15.45,21.18,26.45,25.82,25.91,22.73,16.09,17.64,18.27,35.45,31.27,22.45,26.64,26.59,40.69,39.36,47.27,44.81,33.17,32.17,38.36,39.81,35.17,33.08,40.90,44.99,47.45,23.81,33.69,41.99,38.99,44.
59!
>  ,46.82,39.90,43.19,33.89,13.57,
> 27.35,-51.71,34.38,33.25,19.38,33.05,46.65,45.40,51.80,39.20,38.20,47.20,33.30,32.80,58.90,57.50,57.60,54.20,57.30,54.20,57.30,38.27,38.91,48.55,46.18,30.73,25.82,25.55,39.91,50.45,35.18,30.45,30.82,21.91,44.73,50.09,45.64,37.27,39.45,60.27,62.45,64.64,65.59,90.69,80.36,54.27,59.81,64.17,66.17,53.36,38.81,32.17,53.08,37.90,33.99,37.45,40.81,56.69,35.99,34.99,43.59,53.92,60.72,64.01,50.71,54.15,56.38,3.05,23.52,43.00,40.03,29.59,46.00,46.30,51.52,69.93,53.73,43.37,46.90,60.85,77.61,68.17,66.64,77.04,81.18,31.68,78.85,73.41,89.58,64.58,50.15,53.52,40.95,59.24,47.83,35.71,33.65,41.31,46.26,61.64,34.87,47.56,34.87,23.34,18.96,34.29,24.79,30.10,16.40,27.16,11.92,-11.71,31.22,23.09,28.17,38.76,39.60,44.68,40.28,44.86,47.29,51.87,56.81,60.51,42.97,48.51,33.85,24.10,4.79,4.72,-16.47,-14.64,-14.41,-3.89,1.14,-2.22,-8.31,-14.71,-0.63,-1.75,-14.13,-6.95,9.65,3.40,12.80,-1.80,-1.80,9.20,-5.70,-5.20,16.90,14.50,17.60,21.20,22.30,21.20,22.30,-0.73,-1.09,12.55,7.18,-5.27,-8.18,-9.45,3.91
,1!
>  0.45,-4.82,-3.55,-8.18,-32.09,6.73,11.09,1.64,3.27,1.45,22.27,24.45,30.64,-32.41,-34.31,-19.64,-50.73,-45.19,-36.83,-34.83,-53.64,-62.19,-66.83,-50.92,-62.10,-67.01,-63.55,-59.19,-44.31,-59.01,-54.01,-55.41,-47.84,-41.96,-40.19,35.35,12.57,18.54,23.38,29.91,-10.36,31.24,29.30,103.89,1.66,-34.99,-17.01,-10.43,-14.63,-7.00,30.05,-57.81,-57.81,35.92,25.51,14.33,13.31,6.46,5.44,24.05,3.36,25.82,17.36,-4.63,-0.75,-12.63,-4.45,3.15,-1.60,3.80,-4.80,-2.80,1.20,-10.70,-12.20,5.90,5.50,6.60,12.20,11.30,12.20,11.30,-2.73,-2.09,4.55,13.18,-4.27,-8.18,-11.45,-0.09,6.45,-8.82,-8.55,-9.18,-12.09,0.73,3.09,0.64,3.27,-3.55,11.27,14.45,22.64,18.59,28.69,30.36,9.27,13.81,19.17,19.17,7.36,-1.19,-2.83,10.08,0.90,-6.01,-2.55,2.81,13.69,1.99,4.99,-2.41,6.25,13.04,13.29,26.85,-1.53,6.48,7.38,-27.86,-8.91,7.69,-5.34,-1.46,8.61,10.75,-23.28,-87.47,-24.73,-30.51,-26.38,-21.26,-3.59,-19.13,-17.61,-7.21,-0.02,81.98,-5.88,-13.45,-2.29,17.14,12.77,11.87,27.04,32.83,14.71,7.77,4.80,8.80,10.70,27.00,63.
63!
>  ,16.58,-0.69,-7.64,
> -12.03,-8.89,79.15,64.65,75.34,96.50,81.26,77.45,-3.07,-11.20,3.03,7.52,2.26,7.34,5.99,1.42,6.91,2.34,7.28,10.98,-1.12,-5.58,48.76,82.01,61.94,60.34,50.13,54.71,56.46,63.62,66.21,63.16,60.42,54.63,3.38,-9.75,-2.13,-11.95,-10.35,-11.60,-8.20,-11.80,-9.80,-7.80,-12.70,-11.20,-8.10,-10.50,-9.40,-1.80,-4.70,-1.80,-4.70,-2.73,-6.09,-1.45,-1.82,-1.27,-4.18,-9.45,-6.09,-5.55,-8.82,-10.55,-6.18,-15.09,-7.27,-7.91,-9.36,-7.73,-5.55,-3.73,1.45,6.64,5.59,2.69,14.36,11.27,6.81,11.17,5.17,3.36,0.81,1.17,1.08,1.90,-4.01,-3.55,-2.19,4.69,0.99,3.99,-6.41,-4.61,-0.86,-0.31,8.37,-16.96,-8.95,-5.63,-26.53,-9.11,-7.00,-4.17,-6.08,3.92,5.75,-11.28,-2.31,-15.47,9.83,9.09,12.68,20.00,5.06,11.16,20.03,27.22,-6.42,6.61,1.79,21.91,5.14,9.90,11.75,5.59,23.56,8.50,22.89,5.60,7.16,4.49,24.44,2.85,14.93,-3.85,-6.24,-12.45,-19.37,-35.27,-31.48,-29.94,14.05,-57.85,-44.60,-69.20,-40.80,-29.80,-65.80,-52.70,-50.20,-64.10,-62.50,-60.40,-55.80,-59.70,-55.80,-59.70,-38.73,-32.09,-37.45,-36.82,-15.27,-27.18,-2
2.!
>  45,-29.09,-51.55,-21.82,-29.55,-31.18,-30.09,-42.27,-39.91,-39.36,-33.73,-46.55,-51.73,-48.55,-46.36,-42.41,-50.31,-40.64,-28.73,17.81,-18.83,-37.83,-33.64,-19.19,-12.83,-48.92,-20.10,-27.01,-33.55,-17.19,-27.31,-29.01,-30.01,-31.41,-39.00,-45.92,-44.76,-47.35,-72.68,-63.45,-56.63,18.33,-25.20,-46.26,-30.33,-30.71,-19.79,-22.54,-5.13,9.02,-19.68,-7.18,-8.22,-17.13,-15.61,-20.18,-21.70,-14.35,-16.31,-49.09,-38.80,-43.93,-35.34,-43.57,-30.58,-7.39,3.21,-1.68,-22.23,-16.06,-18.12,12.40,-37.51,-9.94,-26.96,-14.88,-24.52,-26.91,-38.30,-66.25,-86.42,-82.63,-81.08,-49.81,-1.04,-50.57,-48.79,-8.15,-39.65,-50.39,-51.08,-55.14,-47.35,-44.30,-49.48,-51.00,-67.39,-67.60,-21.89,-30.35,-41.01,-56.76,-26.45,-26.83,-8.39,-16.92,-16.69,-25.99,-34.07,-28.28,-22.49,-13.95,-34.95,-3.85,-0.60,-17.20,1.20,15.20,-5.80,3.30,4.80,-6.10,-6.50,-6.40,0.20,-3.70,0.20,-3.70,13.27,15.91,13.55,11.18,28.73,18.82,21.55,15.91,0.45,19.18,18.45,18.82,14.91,5.73,5.09,-1.36,15.27,7.45,4.27,8.45,10.64,10.59,17.
69!
>  ,17.36,26.27,-28.19,
> 33.17,13.17,21.36,25.81,33.17,9.08,25.90,19.99,19.45,22.81,21.69,20.99,16.59,14.34,8.95,12.55,11.07,-17.82,-6.76,-1.63,3.09,20.52,5.25,16.91,13.79,29.58,28.37,29.92,-6.22,32.44,40.07,37.19,32.55,7.25,33.16,33.16,40.51,38.56,5.78,16.98,12.45,21.05,10.38,25.81,35.28,33.69,47.09,26.54,31.18,29.12,27.64,17.35,42.79,23.33,38.46,22.72,21.86,10.47,-5.29,-31.55,-27.76,-26.22,-5.74,-11.83,-30.88,-1.67,-6.75,4.42,5.87,3.66,-0.40,1.30,4.34,-3.89,0.69,-15.71,-8.96,-27.49,-29.95,-41.61,-49.36,-40.29,-38.23,-25.28,-23.15,-25.36,-33.74,-33.28,-32.67,-29.62,-25.97,-28.73,-33.09,-39.45,-39.82,-35.27,-14.18,-7.45,-26.09,-24.55,-13.82,-2.55,-9.18,-17.09,-19.27,-29.91,-15.36,-14.73,-10.55,-55.73,-78.55,-99.36,-87.64,-50.73,-50.19,-77.83,-43.83,-20.64,-12.19,-7.83,-5.92,7.90,18.99,14.45,-2.19,-2.31,-6.01,-7.01,-10.41,-32.47,-47.01,-58.35,-89.59,-92.63,-52.20,-73.87,-68.61,-65.64,-62.95,-54.11,-36.70,14.03,-42.41,-40.88,-44.06,-53.54,-61.60,-49.09,-52.21,-75.94,-69.96,-83.98,-64.87,-90.85,-83.5
4,!
>  -90.07,-79.64,-70.42,-61.85,-70.61,-42.12,-68.59)
> 
> breaks<-c(-150,-140,-130,-120,-110,-100,-90,-80,-70,-60,-50,-40,-30,-20,-10,0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150)
> hist(d.resid,breaks,freq=FALSE,main="Field 7: Ground Water Table Depth
> Residuals (Avg. - Measured Depth)",xlab="Residuals (cm)", col="grey")
> plot(function(d.resid)
> dnorm(d.resid,m=-4.007e-15,s=35.571),-150,150,n=400,add=TRUE,col="black",lwd=3)
> 
> http://n4.nabble.com/file/n998495/Image2.png 
> 
> I would like to do the same thing with the following lines of code, only
> this time fit a log-normal distribution.  
> 
> app_depths<-c(2.133069498, 1.633840467, 4.946905858, 3.74316825, 2.29580986,
> 3.156489013, 3.616272192, 20.65750905, 2.878995473, 12.59126936, 14.2215439,
> 7.892111284, 4.656569671, 3.350734491, 5.457109794, 3.580297328,
> 12.66226362, 4.491926672, 7.1, 13.5, 11.1, 14.1, 7.3, 8.6, 3.5, 5, 7.1, 7,
> 15.1, 9.1, 6.5, 13.4, 15.2, 10.5, 11.8, 8.8, 8, 9, 20.4, 15.6, 14.6, 7.1,
> 3.6, 4.8, 6.4, 10.1, 9.5, 24.5, 16.1, 6.5, 3.3, 3.7, 3.3, 5.3, 4, 2.4, 5.2,
> 3.7, 3.5, 7, 5.6, 5.9, 8.2, 5.2, 12.7, 6.3, 6.6, 1.9, 2.2, 4.1, 4.7, 
> 7.9, 14.4, 16.2, 8.8, 14.1, 12.7, 15.1, 20.2, 4.9, 7.9, 5, 7.9, 4.6, 5.7,
> 32.3, 7.7, 16, 13.9, 16.8, 7.7, 13.6, 5.1, 11.4, 4.59, 7.35, 4.14, 3.82,
> 1.95, 3.06, 7.4, 8.8, 11, 2, 6.6, 7, 5.7, 5.8, 6.2, 2.9, 7.8, 5.7, 12.2,
> 5.8, 5.8, 26.7, 22, 13.5, 7.9, 5.64, 2.03, 1.7, 2.78, 5.78, 2.76, 24.6, 15,
> 12.32, 23.79, 14.3, 11.7, 9.1, 9.4, 8.24, 7.1, 5.9, 4.6, 4.3, 2.96, 7.9,
> 11.13, 5.3, 5.8, 1.7, 7.12, 8.3, 7.83, 6.62, 42.67, 34.76, 41.68, 6.07,
> 4.43, 1.2, 1.8, 1.1, 1.1, 1.3, 1.46, 1.46, 10.31, 2.96, 
> 22.17, 2.75, 3.6, 9.8, 9.68, 5.8, 7.23, 10.06, 4.117439286, 11.63456013,
> 9.92, 7.40, 9.78, 4.37, 7.12, 7.57, 27.03, 3.43, 6.71, 7.74, 10.01, 7.78,
> 14.95, 9.98, 8.07, 10.73, 8.82, 15.08, 12.72, 4.27, 10.07, 6.45, 16.67,
> 16.28, 7.21, 21.46, 21.52, 14.93, 9.03, 26.70, 14.90, 6.12, 17.88, 13.72)
> 
> breaks<-c(0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44)
> hist(app_depths,breaks,freq=FALSE,main="LARV Irrigation Application Depths
> (in) 2004-2007",xlab="Application Depth (in)",col="grey")
> plot(function(app_depths)
> dlnorm(app_depths,meanlog=mean(app_depths),sdlog=sd(app_depths)),0,44,add=TRUE,col="black",lwd=3)
> 
Don't you need meanlog=mean(log()), etc?
I usually use curve():

hist(app_depths,breaks,freq=FALSE)
curve(dlnorm(x, meanlog=mean(log(app_depths)),
    sdlog=sd(log(app_depths))), add=TRUE, col=1, lwd=3)

  -Peter Ehlers

> Problem is, I get a plot that doesn't look right,
> 
> http://n4.nabble.com/file/n998495/Image3.png 
>  
> If there is an obvious error as to why the pdf line doesn't fit the data,
> I'm unable to find it.  Any help would be greatly appreciated.
> 
> Respectfully,
> Eric

-- 
Peter Ehlers
University of Calgary
403.202.3921



[Rd] log normal overlay
Peter Ehlers