r - Creating a curve to fit x-y data where X is categorical -
i've got dataset of diving behavior tagged animals, , i'm struggling fit curve data, think because x variable in case categorical, , not continuous data. let me give bit of background:
my dataset has 184 observations of 14 variables:
tagid ddmmyy hour.gmt.hour.local. x0 x3 x10 x20 x50 x100 x150 x200 x300 x400 1 122097 250912 0 9 0.0 0.0 0.3 12.0 15.3 59.6 12.8 0.0 0 0 2 122097 260912 0 9 0.0 2.4 6.9 5.5 13.7 66.5 5.0 0.0 0 0 3 122097 260912 6 15 0.0 1.9 3.6 4.1 12.7 39.3 34.6 3.8 0 0 4 122097 260912 12 21 0.0 0.2 5.5 8.0 18.1 61.4 6.7 0.0 0 0 5 122097 280912 6 15 2.4 9.3 6.0 3.4 7.6 21.1 50.3 0.0 0 0 6 122097 290912 18 3 0.0 0.2 1.6 6.4 41.4 50.4 0.0 0.0 0 0
the variables i'm interested in x0:x400
. these depth bins, , values represent percent of total time period of day animal spent in depth bin. on first line, spent 0% of time between 0-3meters, 59.6% of time between 100-150 meters, etc. bit of answers last question here on stackoverflow, calculated mean % time spent in each depth bin animal:
diving.means <- colmeans(diving[, -(1:4)]) lowerintervalbound <- gsub("x", "", names(diving)[-(1:4)]) lowints <- as.numeric(lowerintervalbound) plot(x=factor(lowints), y=diving.means, xlab="depth bin (meters—lower bound)", ylab="% time spent")
which provided me plot:
unfortunately because data means (a single value), , not frequencies, couldn't figure out how plot them histogram... that's neither here nor there, can input these values , make desired plot if necessary.. trick analytically now.
now i've got multiple animals , different time bins i'd compare. i'll work out system weight time spent in bins average depth compare statistically, want compare them visually, qualitatively, produce plots can use in presentations , publications. i'd create density curve representing 'histogram,' , plot curves multiple scenarios on single plot compare. however, can't seem make work density()
function, don't have frequency data. sort of have densities calculated already, % time spent in each bin.. they're not represented in raw format in dataset frequencies of categories, can make histograms , density curves out of.
this how data look:
> diving.means x0 x3 x10 x20 x50 x100 x300 x400 x150 x200 3.330978261 3.299456522 8.857608696 17.646195652 30.261413043 29.356521739 6.445108696 0.664130435 0.135869565 0.001630435
or:
> df<-data.frame(lowints, diving.means) > df lowints diving.means x0 0 3.330978261 x3 3 3.299456522 x10 10 8.857608696 x20 20 17.646195652 x50 50 30.261413043 x100 100 29.356521739 x150 150 6.445108696 x200 200 0.664130435 x300 300 0.135869565 x400 400 0.001630435
and produce looks more or less (pulled randomly publication—axes unrelated data):
and able isolate curves , plot them together.
thanks can provide!
you have frequencies, hist
cannot used. can use plot
spline interpolation density:
df <- read.table(text=" lowints diving.means x0 0 3.330978261 x3 3 3.299456522 x10 10 8.857608696 x20 20 17.646195652 x50 50 30.261413043 x100 100 29.356521739 x150 150 6.445108696 x200 200 0.664130435 x300 300 0.135869565 x400 400 0.001630435") require(splines) dens <-predict(interpspline(df[,1], df[,2])) plot(df[,1], df[,2], type="s", ylim=c(0,40)) lines(dens, col="red",lwd=2)
Comments
Post a Comment