Re: Don't know why I'm receiving units>0 error message running Tobit

1,696 views
Skip to first unread message

Winston Chang

unread,
Nov 28, 2012, 3:22:37 PM11/28/12
to Sam Schramski, ggplot2
Hi - 

Could you provide a (simple) reproducible example? See

-Winston


On Wed, Nov 28, 2012 at 1:10 PM, Sam Schramski <sam.sc...@gmail.com> wrote:
Can't figure out why I'm receiving this error message!

p + stat_bin(binwidth = 1) + stat_function(fun = f, size = 1, args = list(var = TobitTimeH3$PctWeakTies, bw = 1))
Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0

Thanks

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

SCS

unread,
Nov 28, 2012, 3:42:18 PM11/28/12
to ggp...@googlegroups.com
# Following instructions for a Tobit on http://www.ats.ucla.edu/stat/r/dae/tobit.htm
# My CSV was attached in the last email
# knitr and ggplot2
library(knitr)
library(ggplot2)
TobitTimeH3 <- read.csv("DissTimeChapterAnalysisH3RTobit.csv")
# function that gives the density of normal distribution for given mean
# and sd, scaled to be on a count metric for the histogram: count =
# density * sample size * bin width
f <- function(x, var, bw = 15) {dnorm(x, mean = mean(var), sd(var)) * length(var) * bw}
# Plot PctWeakTikes as the independent variable and NR_score as the dependent variable
p <- ggplot(TobitTimeH3, aes(x = PctWeakTies, fill = NR_score))
# Attempt to obtain the first histogram and the subsequent error message

p + stat_bin(binwidth = 1) + stat_function(fun = f, size = 1, args = list(var = TobitTimeH3$PctWeakTies, bw = 1))
Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0

Thanks.

Dennis Murphy

unread,
Nov 28, 2012, 4:16:54 PM11/28/12
to SCS, ggp...@googlegroups.com
Hi:

After looking at Josh's nicely done example at the UCLA site, I made a
couple of changes to your code; in particular, your bandwidth needs to
be reduced because the response ranges from 0 to 1. If you set the
bandwidth to 1, you'll only get one or two histogram bars and it's
pointless to draw a density plot over it.

p <- ggplot(TobitTimeH3, aes(x = PctWeakTies, fill = factor(NR_score)))
p + stat_bin(binwidth = 0.1) +
stat_function(fun = f, size = 1,
args = list(var = TobitTimeH3$PctWeakTies, bw = 0.1))

NR_score was converted to a factor; once that was done, the error
message disappeared. However, I wonder about the purpose of using 12
distinct fill colors in a single histogram. Perhaps some form of
aggregation of scores is in order (see the cut() function).

Dennis
Reply all
Reply to author
Forward
0 new messages