Comment calculer la probabilité du graphique de densité?
J'ai la question suivante: est-il possible de calculer une probabilité d'un graphique de densité?
Donc, par exemple, j'ai la trame de données suivante
test<- data.frame(
Gruppe = rep(c("Aktien","Aktien"),
times=c(136, 37)),
Zufriedenheit = c(f_keineErf, f_Erf))
et je trace un graphique de densité, avec la fonction de ggplot:
ggplot(test, aes(x=Zufriedenheit)) +geom_density()
Comment puis-je calculer la probabilité par exemple d'obtenir une valeur supérieure à 70?
Je vous remercie!
Vos données ne sont pas incluses dans la question, alors constituons un petit échantillon aléatoire:
library(ggplot2)
set.seed(69)
df <- data.frame(x = rnorm(10))
Maintenant, nous pouvons créer un graphique de densité selon votre exemple:
p <- ggplot(df, aes(x)) +
geom_density() +
xlim(c(-5, 5))
p
Maintenant, nous pouvons en fait trouver les coordonnées x et y de cette ligne en utilisant la fonction de base R density
et en extrayant ses composantes x et y dans un bloc de données:
dens <- density(df$x)
d <- data.frame(x = dens$x, y = dens$y)
head(d)
#> x y
#> 1 -3.157056 0.0009453767
#> 2 -3.144949 0.0010145927
#> 3 -3.132841 0.0010870523
#> 4 -3.120733 0.0011665920
#> 5 -3.108625 0.0012488375
#> 6 -3.096517 0.0013382316
Nous pouvons voir le tracé comme un pointillé rouge, geom_line
c'est la même chose que geom_density
:
p + geom_line(data = d, aes(x, y), col = "red", linetype = 2, size = 2)
Now suppose we want to know the probability of having a value of more than one. We can show the area we are interested in like this:
p + geom_area(data = d[d$x >= 1,], aes(x, y), fill = "red")
Since the x values are all equally spaced in our data frame d
, then the red area's proportion of the area under the line is a simple ratio of the sum of all y values at x values greater than one to the grand sum of y:
sum(d$y[d$x > 1])/sum(d$y)
#> [1] 0.1599931
So the probability of getting an x value of > 1 is 0.15999, or 16%
Created on 2020-08-17 by the reprex package (v0.3.0)