Poisson distribution too narrow, negative binomial too broad - r

I'm trying to fit some count data for the number of fish purchased by anglers(grey in the image) with a distribution using optim in R. I've fit both a poisson (red) and negative binomial distribution (blue) but as you can see neither seems to be right. What should my next steps be for getting a better fit?
My graph:
#fit poisson curve to data using optim
minus.logL.s<-function(lambda, dat){
-sum(dpois(dat,lambda, log=TRUE))}
mle<-optim(par=45,fn=minus.logL.s, method="BFGS",hessian=T,dat=survey.responses.baitusers$fish.per.trip)
mle
#simulate data coming from a poisson distribution of mean 38
simspois<-as.data.frame(rpois(1000, 38))
colnames(simspois)<-("simulated_values")
#fit negative binomial curve
minus.logL.nb<-function(pars, dat){
mu<-pars[1]
size<-pars[2]
-sum(dnbinom(dat, mu=mu, size=size,log=TRUE))}
mlenb<-optim(par=c(mu=38,size=1),fn=minus.logL.nb, method="BFGS",hessian=T,dat=survey.responses.baitusers$fish.per.trip)
mlenb
simsnegbin<-as.data.frame(rnbinom(1000,size=4, mu=38))
colnames(simsnegbin)<-("simulated_valuesnb")
#graph both
graph<-ggplot(survey.responses.baitusers)+aes(fish.per.trip)+geom_histogram()+geom_smooth(data=simspois, aes(simulated_values), stat = "count",color="red")+geom_smooth(data=simsnegbin, aes(simulated_valuesnb), stat="count", color="blue")
graph
Output from negative binomial fitting:
$par
mu size
38.333338 4.107287
Output from poisson fitting:
$par
[1] 38.33333
My data:
> survey.responses.baitusers$fish.per.trip
[1] 15 34 42 38 8 38 21 29 58 29 40 35 33 51 50 40 8 45 44 45 34 57 8 28 63 54 22 44 65 54 54 15 12
[34] 42 59 40 43 95 80 15 54 19 44 27 53 95 21 38 40 13 25 27 79 38 85 40 33 74 34 77 34 34 33 35 89 34
[67] 34 37 16 60 17 21 18 37 34 27 30 62 48 35 55 50 23 32 56 34 11 21 34 48 15 34 26 54 8 95 8 58 54
[100] 44 34 47 35 13 21 53 52 52 40 40 33 8 15 15 25 41 63 34 38 87 14 68 58 59 34 55 24 24 35 33 21 8
[133] 8 15 51 48 8 21 39 29 50 54 62 16 54 33 58 22 49 40 30 51 21 19 51 40 34 27 40 45 80 69 8 42 33
[166] 62 40 82 17 14 30 61 45 70 33 33 16 49 32 34 31 31 18 64 33 39 21 56 40 52 71 34 30 27 54 8 64 16
[199] 54 127 13 51 40 33 63 31 30 63 56 57 77 46 64 22 34 50 66 33 34 59 45 16 21 60 58 15 64 29 40 44 29
[232] 8 21 16 72 34 49 57 34 34 15 33 54 40 32 33 95 107 49 64 59 64 37 70 45 16 16 40 19 53 34 39 21 36
[265] 34 17 8 34 51 13 20 34 21 38 36 36 41 34 83 27 8 45 29 34 21 37 44 15 50 25 27 8 27 19 24 40 8
[298] 28 36 24 40 21 70 20 34 21 46 16 20 8 33 34 54 44 77 80 15 34 40 29 48 59 29 8 15 47 45 21 41 23
[331] 34 51 14 40 25 45 64 59 107 21 59 27 56 48 34 45 59 35 30 37 32 8 51 11 48 64 32 8 52 14 20 18 8
[364] 53 52 53 33 34 48 62 34 34 8 46 39 21 33 34 40 49 52 19 24 29 43 19 29 27 46 52 29 51 61 16 17 35
[397] 34 40 25 28 34 42 66 35 49 35 51 66 21 51 45 14 53 22 42 64 8 48 28 66 52 40 29 34 34 41 59 34 52
[430] 16 32 20 35 8 8 21 49 40 33 16 24 8 42 23 63 26 21 33 8 23 112 57 8 46 18 67 34 30 33 40 43 57
[463] 60 33 14 27 44 21 31 30 27 49 57 69 66 22 28 55 11 43

Related

ifelse() not working correctly using mutate: R

I have a small doubt in ifelse confition. I've read numerous articles and tried different solutions but I'm not able to solve it. Please help me with it.
I have a column of numbers ranging from 0:59 and it recurs like this for 500 rows.
I am trying to create another column which divides 0:29 as 0 and 30:59 as 30.
Data:
> data$Minute
[1] 0 0 0 1 1 1 2 2 2 2 2 2 3 3 4 4 4 5 5 5 5 5 6 6 6 7 7 7 7 7 8 8 8 8 8 8 9
[38] 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 12 12 12 12 13 13 13 13 13 14 14 14 14 14 14 15 15 15
[75] 15 15 15 15 15 15 16 16 16 16 16 17 17 18 18 18 18 18 19 19 19 19 20 20 20 20 20 20 20 20 21 21 21 21 21 21 21
[112] 22 22 22 22 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 24 24 24 24 24 24 24 24 25 25 25 25 25 25 25 25 25
[149] 26 26 26 26 26 26 26 26 26 26 27 27 27 27 27 27 27 27 27 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 29
[186] 29 29 29 29 29 29 29 29 29 29 29 30 30 30 30 30 30 30 30 30 30 30 30 30 31 31 31 31 31 31 31 31 31 31 31 31 31
[223] 31 32 32 32 32 32 33 33 33 33 33 33 33 33 33 33 33 34 34 34 34 34 34 34 34 34 34 34 34 34 35 35 35 35 35 35 35
[260] 35 35 35 36 36 36 36 36 36 36 36 37 37 37 37 37 37 37 37 37 38 38 38 38 38 38 38 38 38 38 39 39 39 39 39 39 39
[297] 39 39 40 40 40 40 40 40 40 40 40 40 40 40 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 42 42 42 42 42 42 42
[334] 42 42 42 42 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 44 44 44 44 44 44 44 44 44 45 45 45 45 45 45 45 45
[371] 45 45 45 45 45 45 45 45 46 46 46 46 46 46 46 47 47 47 47 47 47 47 47 47 47 47 47 47 48 48 48 48 48 48 48 48 48
[408] 48 48 48 49 49 49 49 49 49 49 50 50 50 50 50 50 50 50 50 50 50 50 50 50 51 51 51 51 51 51 51 51 51 51 52 52 52
[445] 52 52 52 52 52 52 52 52 52 53 53 53 53 53 53 53 53 54 54 54 54 54 54 54 54 54 54 54 55 55 55 55 55 55 55 55 55
[482] 55 56 56 56 56 56 56 56 56 56 56 56 56 56 57 57 57 57 57 57 57 57 58 58 58 58 58 58 58 58 58 59 59 59 59 59 59
[519] 59 59 59 59 59 59 59 0 0 0 0
Code:
data<- data %>% mutate(Period = ifelse(((Minute >=0) && (Minute <= 30)),0, 30) )
While running this code, I am only getting Period value as 0 for all the data points. Can you please help me with this small issue?
Thanks in Advance!
Try this
data %>% mutate(Period = ifelse(minute <= 30, 0, 30))

ANOVA test on groups with replicates

I have 3 independent groups and want to know which of these group means are different.
My dataset looks as below
T1 T2 T3 T4 H1 H2 H3 S1 S2 S3
A 22 19 16 13 10 19 16 13 10 7
B 55 52 49 46 43 52 49 46 43 40
C 26 23 20 17 14 23 20 17 14 11
D 84 81 78 75 72 81 78 75 72 69
E 95 92 89 86 83 92 89 86 83 80
F 45 42 39 36 33 42 39 36 33 30
G 35 32 29 26 23 32 29 26 23 20
H 84 81 78 75 72 81 78 75 72 69
I 39 36 33 30 27 36 33 30 27 24
I am aware of how to do ANOVA for single vector group, but here I have 3 (T, H and S) groups with replicates, can someone please help how to do the one way ANOVA test for the above data.

AIC for probability density function

I want code in r on how to calculate the AIC CAIC BIC HQIC W A of this this pdf
f(x) = ((a*log(b))/(x^2*b - x^2))*exp(-(a/x))*b^(exp(-(a/x))) and cdf F(x)= (b^(exp(-(a/x))) - 1)/(b - 1)
using these data
1 11 4 32 23 45 115 37 29 71 39 23 21 37 20 12 13 135
49 32 64 40 77 97 97 85 10 27 7 48 35 61 79 63 16 80
108 20 52 82 50 64 59 39 9 16 78 35 66 122 89 110 44 28
65 22 59 23 31 44 21 9 45 168 73 76 118 84 85 96 78 73
91 47 32 20 23 21 24 44 21 28 9 13 46 18 13 24 16 13
23 36 7 14 30 14 18 20

reprex setting output width

How do I set the width of a reprex output?
Say I have a code like this:
(x <- 1:100)
I get this with reprex::reprex(venue = "so")
(x <- 1:100)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#> [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#> [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#> [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#> [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#> [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
How can I increase the width of the output to output something like this
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
[51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Possible Solutions
One option that I have found but I find rather "un-tidy" is this (include options(width = ...) at the top of the code. But I don't want it to show up in the output, I'd prefer setting the width in the reprex-call.
options(width = 205)
(x <- 1:100)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#> [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
reprex() allows for knitr's opts-chunk, but I can't get it working with reprex::reprex(venue = "so", opts_chunk = list(out.width = 205)) (which might be related to #421 as pointed out here (Long lines of text output))
Any better solutions?
reprex has a syntax for setting these options but not including them in the output markdown (see here for examples). In this case:
reprex({
#+ setup, include = FALSE
options(width=205)
#+ actual-reprex-code
(x <- 1:100)
}, venue = 'so')
outputs your desired format:
(x <- 1:100)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#> [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Created on 2018-09-21 by the reprex package (v0.2.1)

R: Trimming/ Setting Boundaries on Filled.contour plot

I made a filled.contour plot and it cam out nicely, however I would like to trim out some of the hypotenuse of the triangle. Basically get rid of some of that yellow stripe along the hypotenuse.
Is there anyway I could go about doing that in R?
Here is my code:
library(akima)
> attach(asc)
>
> test<-interp(AvgDepth, AvgMaxDepth, Gs)
>
> filled.contour(test, color=heat.colors, xlab="Depth (m)", ylab="Maximum Depth (m)", ylim=c(5,90))
Here is my data set
AvgDepth AvgMaxDepth Gs
1 5 5 0.022706473
2 5 15 -0.006287207
3 15 15 -0.002071806
4 5 25 -0.002569846
5 15 25 -0.005698020
6 25 25 -0.013394740
7 5 35 -0.001723604
8 15 35 -0.004575939
9 25 35 -0.001260225
10 35 35 0.025808307
11 5 45 -0.008369802
12 15 45 -0.004661506
13 25 45 0.003438334
14 35 45 0.004066056
15 5 55 -0.004517855
16 15 55 0.001577937
17 25 55 -0.000761080
18 35 55 0.004597452
19 45 55 0.015894575
20 5 65 -0.003023326
21 15 65 0.001327518
22 25 65 -0.000967222
23 35 65 -0.005843258
24 45 65 -0.000534109
25 55 65 0.001292299
26 5 75 -0.003593511
27 15 75 0.000484908
28 25 75 -0.008013139
29 35 75 -0.013281240
30 45 75 -0.009767021
31 55 75 -0.019364488
32 65 75 -0.019202670
33 5 85 -0.004487259
34 15 85 -0.001588138
35 25 85 -0.004464418
36 35 85 -0.007797982
37 45 85 -0.013272495
38 55 85 -0.022616793
39 65 85 -0.017740362
40 75 85 0.012021166
41 5 95 0.002236271
42 15 95 0.002102761
43 25 95 -0.001748743
44 35 95 -0.003063959
45 45 95 -0.001264025
46 55 95 -0.004662023
47 65 95 0.002980029
48 75 95 0.015868836
49 85 95 0.008842697
50 95 95 0.036387641
Any Help is appreciated.
Thanks

Resources