[R] tapply and error bars

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Sun Jun 24 12:24:43 CEST 2018


Hi Ogbos,
If I use the example data that you sent, I get the error after this line:

oose<-as.vector(by(oodf$B,oodf$A,std.error))
Error in FUN(X[[i]], ...) : object 'std.error' not found

The reason is that you have not defined std.error as a function, but
as the result of a calculation. When I rewrite it like this:

std.error<-function(x) return(sd(x)/(sum(!is.na(x))))
oose<-as.vector(by(oodf$B,oodf$A,std.error))
plot(-5:10,oomean,type="b",ylim=c(50000,110000),
 xlab="days (epoch is the day of Fd)",ylab="strikes/km2/day")
dispersion(-5:10,oomean,oose)

I get the expected plot.

Jim


On Sat, Jun 23, 2018 at 9:36 PM, Ogbos Okike <giftedlife2014 using gmail.com> wrote:
> Hi Jim,
>
> Thanks for assisting. Here is what I did:
>
> A<-matrix(rep(-5:10,71))
> B<-matrix(data)
> std.error = sd(B)/sqrt(sum(!is.na(B)))
>  oodf<-data.frame(A,B)
>
>  oomean<-as.vector(by(oodf$B,oodf$A,mean))
> oose<-as.vector(by(oodf$B,oodf$A,std.error))
> plot(-5:10,oomean,type="b",ylim=c(50000,110000),
>  xlab="days (epoch is the day of Fd)",ylab="strikes/km2/day")
> dispersion(-5:10,oomean,oose)
>
> And the error says:
> Error in FUN(X[[1L]], ...) : could not find function "FUN"
>
> Please note that I use:
> std.error = sd(B)/sqrt(sum(!is.na(B)))
>  to calculate the standard error as it requested for it.
>
> Thanks
> Ogbos
>
> On Sat, Jun 23, 2018 at 10:09 AM, Jim Lemon <drjimlemon using gmail.com> wrote:
>>
>> Hi Ogbos,
>> This may help:
>>
>> # assume your data frame is named "oodf"
>> oomean<-as.vector(by(oodf$B,oodf$A,mean))
>> oose<-as.vector(by(oodf$B,oodf$A,std.error))
>> plot(-5:10,oomean,type="b",ylim=c(50000,110000),
>>  xlab="days (epoch is the day of Fd)",ylab="strikes/km2/day")
>> dispersion(-5:10,oomean,oose)
>>
>> Jim
>>
>> On Sat, Jun 23, 2018 at 4:35 PM, Ogbos Okike <giftedlife2014 using gmail.com>
>> wrote:
>> > Dear workers,
>> > I have a data of length 1136. Below is the code I use to get the means
>> > B.
>> > It worked fine and I had the mean calculated and plotted.
>> >
>> > I wish to plot the error bars as well. I already plotted such means with
>> > error bars before. Please see attached for example.
>> >
>> > I tried to redo the same plot but unlikely could not get around it as I
>> > lost my system containing the script.
>> > Among many attempts, I tried:
>> > library(gplots)
>> >
>> >  plotmeans(errors~AB,xlab="Factor A",ylab="mean errors", p=.68,
>> > main="Main
>> >   effect Plot",barcol="black")
>> > Nothing worked.
>> >
>> > I would really be thankful should somebody return me to the track.
>> > Many, many thanks for your time.
>> > Ogbos
>> >
>> > A sample of the data is:
>> > S/N      A        B
>> > 1    -5  64833
>> > 2    -4  95864
>> > 3    -3  82322
>> > 4    -2  95591
>> > 5    -1  69378
>> > 6     0  74281
>> > 7     1 103261
>> > 8     2  92473
>> > 9     3  84344
>> > 10    4 127415
>> > 11    5 123826
>> > 12    6 100029
>> > 13    7  76205
>> > 14    8 105162
>> > 15    9 119533
>> > 16   10 106490
>> > 17   -5  82322
>> > 18   -4  95591
>> > 19   -3  69378
>> > 20   -2  74281
>> > 21   -1 103261
>> > 22    0  92473
>> > 23    1  84344
>> > 24    2 127415
>> > 25    3 123826
>> > 26    4 100029
>> > 27    5  76205
>> > 28    6 105162
>> > 29    7 119533
>> > 30    8 106490
>> > 31    9 114771
>> > 32   10  55593
>> > 33   -5  85694
>> > 34   -4  65205
>> > 35   -3  80995
>> > 36   -2  51723
>> > 37   -1  62310
>> > 38    0  53401
>> > 39    1  65677
>> > 40    2  76094
>> > 41    3  64035
>> > 42    4  68290
>> > 43    5  73306
>> > 44    6  82176
>> > 45    7  75566
>> > 46    8  89762
>> > 47    9  88063
>> > 48   10  94395
>> > 49   -5  80651
>> > 50   -4  81291
>> > 51   -3  63702
>> > 52   -2  70297
>> > 53   -1  64117
>> > 54    0  71219
>> > 55    1  57354
>> > 56    2  62111
>> > 57    3  42252
>> > 58    4  35454
>> > 59    5  33469
>> > 60    6  38899
>> > 61    7  64981
>> > 62    8  85694
>> > 63    9  79452
>> > 64   10  85216
>> > 65   -5  71219
>> > 66   -4  57354
>> > 67   -3  62111
>> > 68   -2  42252
>> > 69   -1  35454
>> > 70    0  33469
>> > 71    1  38899
>> > 72    2  64981
>> > 73    3  85694
>> > 74    4  79452
>> > 75    5  85216
>> > 76    6  81721
>> > 77    7  91231
>> > 78    8 107074
>> > 79    9 108103
>> > 80   10  7576
>> >
>> > A<-matrix(rep(-5:10,71))
>> > B<-matrix(data)
>> >  AB<-data.frame(A,B)
>> >
>> > x= B
>> >
>> >  f<-factor(A)
>> > AB<- tapply(x,f,mean)
>> > x<--5:10
>> > plot(x,AB,type="l")
>> >
>> > ______________________________________________
>> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
>> >
>
>




More information about the R-help mailing list