Steve_Friedman at nps.gov
2010-Jun-14 15:54 UTC
[R] script development for Unconditional Density and Probability estimation
Hello, I'd like to automate this script a bit more and cycle several parameters(both the species and the metric). For example where AnnualDepth occurs, I need to process about 12 metrics so instead of writing this entire script 12 times once for each metric I'd like to be able to automatically get another metric. Any suggestion will be greatly appreciated. Currently running Windows XP, R 2.11.1 ############################################################################### Marsh <- cbind(SoilVegHydro, vegcode) AnnualDepth <- Marsh[,'meanAnnualDepthAve'] cattail_0 <- Marsh[,'cattail'] == '0' # no need to run for 8 species, automate if possible cattail_1 <- Marsh[,'cattail'] == '1' # yes need to run for 8 species spbase.rate.d1 <- sum(cattail_1)/(sum(cattail_1) + sum(cattail_0) ) annualDepth.density <- density(AnnualDepth) # this line needs either interactively defined or automatically cycle thru a number of metrics cattail.d0.density <- density(AnnualDepth[cattail_0]) cattail.d1.density <- density(AnnualDepth[cattail_1]) approxfun (cattail.d0.density$x, cattail.d0.density$y) -> cattail.d0.f approxfun (cattail.d1.density$x, cattail.d1.density$y) -> cattail.d1.f p.d.given.AnnualDepth <- function(AnnualDepth, spbase.rate.d1) { p1 <- cattail.d1.f(AnnualDepth) * spbase.rate.d1 p0 <- cattail.d0.f(AnnualDepth) * (1 - spbase.rate.d1) p1/(cattail_0+cattail_1) } x <- 1:1292 y <- p.d.given.AnnualDepth(x, spbase.rate.d1) plot (x, y, type='l', col='red', xlab='Mean Annual Depth', main=c("Cattail"), ylab='estimated\nProbability(cattail|AnnualDepth)') plot (cattail.d0.density, col ='red', lty= 1, main = "") lines(cattail.d1.density, col = 'blue', lty=1) lines(annualDepth.density , col='black', lty = 1) legend(2000, 0.0023, c("No Cattail", "Cattail", "Mean Annual Depth"), col=c("red", "blue", "black"),lty=c(1)) ##################################################################################################### Steve Friedman Ph. D. Spatial Statistical Analyst Everglades and Dry Tortugas National Park 950 N Krome Ave (3rd Floor) Homestead, Florida 33034 Steve_Friedman at nps.gov Office (305) 224 - 4282 Fax (305) 224 - 4147
jim holtman
2010-Jun-14 16:28 UTC
[R] script development for Unconditional Density and Probability estimation
First start by putting it in a function so you can specify the parameters you want to change. On Mon, Jun 14, 2010 at 11:54 AM, <Steve_Friedman at nps.gov> wrote:> > Hello, > > I'd like to automate this script a bit more and cycle several > parameters(both the species and the metric). ?For example where AnnualDepth > occurs, I need to process about 12 metrics so instead of writing this > entire script 12 times once for each metric ?I'd like to be able to > automatically get another metric. > > Any suggestion will be greatly appreciated. > > Currently running Windows XP, R 2.11.1 > > ############################################################################### > > Marsh <- cbind(SoilVegHydro, vegcode) > AnnualDepth <- Marsh[,'meanAnnualDepthAve'] > > cattail_0 <- Marsh[,'cattail'] == '0' ?# no ? ? ? ? ? need to run for 8 > species, automate if possible > ?cattail_1 <- Marsh[,'cattail'] == '1' # yes ? ? ? ? need to run for 8 > species > > spbase.rate.d1 <- sum(cattail_1)/(sum(cattail_1) + sum(cattail_0) ) > annualDepth.density <- density(AnnualDepth) ? ?# this line needs either > interactively defined or automatically cycle thru a number of metrics > > cattail.d0.density <- density(AnnualDepth[cattail_0]) > cattail.d1.density <- density(AnnualDepth[cattail_1]) > > approxfun (cattail.d0.density$x, cattail.d0.density$y) -> cattail.d0.f > approxfun (cattail.d1.density$x, cattail.d1.density$y) -> cattail.d1.f > > p.d.given.AnnualDepth <- function(AnnualDepth, spbase.rate.d1) > ? ? { > ? ? ? ?p1 <- cattail.d1.f(AnnualDepth) * spbase.rate.d1 > ? ? ? ?p0 <- cattail.d0.f(AnnualDepth) * (1 - spbase.rate.d1) > ? ? ? p1/(cattail_0+cattail_1) > ? ?} > > x <- 1:1292 > y <- p.d.given.AnnualDepth(x, spbase.rate.d1) > plot (x, y, type='l', col='red', xlab='Mean Annual Depth', > main=c("Cattail"), ylab='estimated\nProbability(cattail|AnnualDepth)') > > plot (cattail.d0.density, col ='red', lty= 1, main = "") > ?lines(cattail.d1.density, col = 'blue', lty=1) > ?lines(annualDepth.density , col='black', lty = 1) > ?legend(2000, 0.0023, c("No Cattail", "Cattail", "Mean Annual Depth"), > ? ? ?col=c("red", "blue", "black"),lty=c(1)) > > > > > ##################################################################################################### > > Steve Friedman Ph. D. > Spatial Statistical Analyst > Everglades and Dry Tortugas National Park > 950 N Krome Ave (3rd Floor) > Homestead, Florida 33034 > > Steve_Friedman at nps.gov > Office (305) 224 - 4282 > Fax ? ? (305) 224 - 4147 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Seemingly Similar Threads
- glm models with more than one response
- Data format question for triangle.plot package ade4
- 17 commits - libswfdec/js libswfdec/swfdec_js.c libswfdec/swfdec_js_global.c libswfdec/swfdec_js.h libswfdec/swfdec_js_movie.c libswfdec/swfdec_movie.c libswfdec/swfdec_player.c libswfdec/swfdec_player_internal.h libswfdec/swfdec_root_movie.c
- 10 commits - libswfdec/js libswfdec/swfdec_js_color.c libswfdec/swfdec_js_connection.c libswfdec/swfdec_js_movie.c libswfdec/swfdec_js_net_stream.c libswfdec/swfdec_js_video.c libswfdec/swfdec_js_xml.c libswfdec/swfdec_script.c player/.gitignore
- Plotting Question