As the title says, I'm trying to plot the CDF of a N(0,1) distribution between some values a, b. I.e. Phi_0,1 (a) to Phi_0,1 (b). For some reason I'm having issues finding information on how to do this.
You can use curve to do the plotting, pnorm is the normal probability (CDF) function:
curve(pnorm, from = -5, to = 2)
Adjust the from and to values as needed. Use dnorm if you want the density function (PDF) instead of the CDF. See the ?curve help page for a few additional arguments.
Or using ggplot2
library(ggplot2)
ggplot(data.frame(x = c(-5, 2)), aes(x = x)) +
stat_function(fun = pnorm)
Generally, you can generate data and use most any plot function capable of drawing lines in a coordinate system.
x = seq(from = -5, to = 2, length.out = 1000)
y = pnorm(x)
Related
I am trying to plot the density of the gamma distribution.
x<-seq(0,10000,length.out = 1000)
plot(density(rgamma(1000,shape = 7,scale = 120)))
plot(dgamma(x,shape=7,scale=120),col="red")
But, I don't understand why both plots are totally different.
Since you didn't supply x in the final call, the x coordinates defaulted to the indices 1,2,3,...1000 of the vector dgamma(x,shape=7,scale=120) rather than the intended 0,10,20,....
If you do:
x<-seq(0,10000,length.out = 1000)
plot(density(rgamma(1000,shape = 7,scale = 120)))
points(x,dgamma(x,shape=7,scale=120),type = "l", col="red")
Then the graph is:
How can I plot a chi-square density graph in R?
I got the following codes but I'm not sure how to manipulate them:
curve( dchisq(x, df=28), col='red', main = "Chi-Square Density Graph",
from=0,to=60)
xvec <- seq(7.5,60,length=101)
pvec <- dchisq(xvec,df=28)
polygon(c(xvec,rev(xvec)),c(pvec,rep(0,length(pvec))),
col=adjustcolor("black",alpha=0.3))
Could someone explain what the codes mean?
The package ggplot2 provides an easy way to plot Chi square distributions. You have to simply specify a stat_function with dchisq as your function and then a list to args that indicates the degrees of freedom.
For example, here is sample code for a Chi square distribution for 4 degrees of freedom:
library(ggplot2)
ggplot(data.frame(x = c(0, 20)), aes(x = x)) +
stat_function(fun = dchisq, args = list(df = 4))
This question already has answers here:
How to plot a function curve in R
(7 answers)
Closed 6 years ago.
I'm doing some research on truncated distributions, specifically on the Truncated Pareto distribution. This has a known density function and probability function, so one is able to design the quantile function and with that a 'random generate numbers' function.
But now that I have these functions, let's say that dtp(x,lower,upper,alpha) is my density function, how do I plot in fact the density? I know that there exists commands like density() which uses kernel estimation, but one should however be able to plot the density function with the aid of the density function itself and with random numbers following said distribution?
The standard way to plot is to have x values and y values, and then plot them. You have a function that converts x values to y values, which means that all you need to do is pick x values to plot and give them to your function, something like:
x = seq(0, 10, length.out = 100)
y = dtp(x = x)
plot(x, y, type = "l")
Note that I have no idea whether this is a reasonable domain for your density, if you have suitable default values for lower, upper, alpha or if you need to specify them, etc.
Alternatively, some functions like curve for base plot just take a function and a domain, you can pass through additional arguments.
curve(dtp, from = 0, to = 10, n = 101)
curve(dtp, from = 0, to = 10, n = 101, alpha = 0.2) # specifying alpha
If you prefer ggplot, then stat_function is the function for you.
library(ggplot2)
ggplot(data.frame(x = c(0, 10), aes(x = x)) +
stat_function(fun = dtp)
ggplot(data.frame(x = c(0, 10), aes(x = x)) +
stat_function(fun = dtp, args = list(alpha = 0.2))
# passing alpha to dtp via args
I want to plot a matrix of z values with x rows and y columns as a surface similar to this graph from MATLAB.
Surface plot:
Code to generate matrix:
# Parameters
shape<-1.849241
scale<-38.87986
x<-seq(from = -241.440, to = 241.440, by = 0.240)# 2013 length
y<-seq(from = -241.440, to = 241.440, by = 0.240)
matrix_fun<-matrix(data = 0, nrow = length(x), ncol = length(y))
# Generate two dimensional travel distance probability density function
for (i in 1:length(x)) {
for (j in 1:length(y)){
dxy<-sqrt(x[i]^2+y[j]^2)
prob<-1/(scale^(shape)*gamma(shape))*dxy^(shape-1)*exp(-(dxy/scale))
matrix_fun[i,j]<-prob
}}
# Rescale 2-d pdf to sum to 1
a<-sum(matrix_fun)
matrix_scale<-matrix_fun/a
I am able to generate surface plots using a couple methods (persp(), persp3d(), surface3d()) but the colors aren't displaying the z values (the probabilities held within the matrix). The z values only seem to display as heights not as differentiated colors as in the MATLAB figure.
Example of graph code and graphs:
library(rgl)
persp3d(x=x, y=y, z=matrix_scale, color=rainbow(25, start=min(matrix_scale), end=max(matrix_scale)))
surface3d(x=x, y=y, z=matrix_scale, color=rainbow(25, start=min(matrix_scale), end=max(matrix_scale)))
persp(x=x, y=y, z=matrix_scale, theta=30, phi=30, col=rainbow(25, start=min(matrix_scale), end=max(matrix_scale)), border=NA)
Image of the last graph
Any other tips to recreate the image in R would be most appreciated (i.e. legend bar, axis tick marks, etc.)
So here's a ggplot solution which seems to come a little bit closer to the MATLAB plot
# Parameters
shape<-1.849241
scale<-38.87986
x<-seq(from = -241.440, to = 241.440, by = 2.40)
y<-seq(from = -241.440, to = 241.440, by = 2.40)
df <- expand.grid(x=x,y=y)
df$dxy <- with(df,sqrt(x^2+y^2))
df$prob <- dgamma(df$dxy,shape=shape,scale=scale)
df$prob <- df$prob/sum(df$prob)
library(ggplot2)
library(colorRamps) # for matlab.like(...)
library(scales) # for labels=scientific
ggplot(df, aes(x,y))+
geom_tile(aes(fill=prob))+
scale_fill_gradientn(colours=matlab.like(10), labels=scientific)
BTW: You can generate your data frame of probabilities much more efficiently using the built-in dgamma(...) function, rather than calculating it yourself.
In line with alexis_laz's comment, here is an example using filled.contour. You might want to increase your by to 2.40 since the finer granularity increases the time it takes to generate the plot by a lot but doesn't improve quality.
filled.contour(x = x, y = y, z = matrix_scale, color = terrain.colors)
# terrain.colors is in the base grDevices package
If you want something closer to your color scheme above, you can fiddle with the rainbow function:
filled.contour(x = x, y = y, z = matrix_scale,
color = (function(n, ...) rep(rev(rainbow(n/2, ...)[1:9]), each = 3)))
Finer granularity:
filled.contour(x = x, y = y, z = matrix_scale, nlevels = 150,
color = (function(n, ...)
rev(rep(rainbow(50, start = 0, end = 0.75, ...), each = 3))[5:150]))
What I want to do sounds simple. I want to plot a normal IQ curve with R with a mean of 100 and a standard deviation of 15. Then, I'd like to be able to overlay a scatter plot of data on top of it.
Anybody know how to do this?
I'm guessing what you want to do is this: you want to plot the model normal density with mean 100 and sd = 15, and you want to overlay on top of that the empirical density of some set of observations that purportedly follow the model normal density, so that you can visualize how well the model density fits the empirical density. The code below should do this (here, x would be the vector of actual observations but for illustration purposes I'm generating it with a mixed normal distribution N(100,15) + 15*N(0,1), i.e. the purported N(100,15) distribution plus noise).
require(ggplot2)
x <- round( rnorm( 1000, 100, 15 )) + rnorm(1000)*15
dens.x <- density(x)
empir.df <- data.frame( type = 'empir', x = dens.x$x, density = dens.x$y )
norm.df <- data.frame( type = 'normal', x = 50:150, density = dnorm(50:150,100,15))
df <- rbind(empir.df, norm.df)
m <- ggplot(data = df, aes(x,density))
m + geom_line( aes(linetype = type, colour = type))
Well, it's more like a histogram, since I think you are expecting these to be more like an integer rounded process:
x<-round(rnorm(1000, 100, 15))
y<-table(x)
plot(y)
par(new=TRUE)
plot(density(x), yaxt="n", ylab="", xlab="", xaxt="n")
If you want the theoretic value of dnorm superimposed, then use one of these:
lines(sort(x), dnorm(sort(x), 100, 15), col="red")
-or
points(x, dnorm(x, 100, 15))
You can generate IQ scores PDF with:
curve(dnorm(x, 100, 15), 50, 150)
But why would you like to overlay scatter over density curve? IMHO, that's very unusual...
In addition to the other good answers, you might be interested in plotting a number of panels, each with its own graph. Something like this.