Normal density curve [duplicate] - r

This question already has answers here:
How to plot a function curve in R
(7 answers)
R plot Probability Density Function
(1 answer)
Distributions and densities in R [duplicate]
(1 answer)
Closed 4 months ago.
I was trying to plot a probability density function curve in R for normal distribution. I have used the following codes but am not getting the normal curve shape in the graph. Anyone can help? This is the code.
#generating normal variables using R
x=rnorm(10,mean=0,sd=0.5)
x
y <- dnorm(rnorm(, mean = 0, sd = 0.5)
plot(x,y, type = "l")

Related

Histogram and distribution function in R [duplicate]

This question already has answers here:
Fitting a density curve to a histogram in R
(7 answers)
Closed 4 years ago.
i would like to plot a histogram of a dataset and the plot of the fitting lognormal distribution - I only see the histogram and not the distribution function...
hist(kmugesamt, prob=TRUE)
curve(plnorm(x, mean=mean(kmugesamt), sd=sd(kmugesamt)), add=TRUE)
Many thanks in advance
Does this help?
k<-rlnorm(1000)
hist(k, freq=F)
curve(dlnorm(x, mean=mean(k), sd=sd(k)), add=TRUE)

overlay normal curve over a histogram [duplicate]

This question already has answers here:
Overlay normal curve to histogram in R
(4 answers)
Closed 4 years ago.
I am supposed to draw an overlay normal curve over a histogram in R. I am using the following code.
g <- unesco$Infant.Deaths
hist(g)
lines(seq(0, 200, by=5), dnorm(seq(0, 200, by=5),
mean(g), sd(g)), col="blue")
But instead of a curve, I am getting a straight line
It can be, that your histogram is representing frequencies instead of probability densities. Try to use hist(g, freq = FALSE).

plotting a quadratic function in R [duplicate]

This question already has an answer here:
Messy plot when plotting predictions of a polynomial regression using lm() in R
(1 answer)
Closed 5 years ago.
Here's how I plotted a quadratic curve:
factor <- 1:7
data <- c(0.1375000,0.2500000,0.3416667,0.4583333,0.7250000,0.9166667,1.0000000)
plot(factor, fitted(lm(data~factor+I(factor^2))), type="l")
I try to do the same with my another data.
factor1<-c(2833,2500,2437,2124,1382,3736,2100,1844,2740,957,1614,1100,1550,3858,2430,2139,1812,1757,1847,945)
data1<-c(0.95,0.88,0.88,0.93,0.81,0.67,0.55,0.53,0.52,0.90,0.87,0.20,0.28,-0.16,0.23,0.11,0.26,0.08,0.73,0.76)
plot(factor1,fitted(lm(data1~factor1+I(factor1^2))), type="l")
I think this is because the second dataset is not sorted but I thought R automatically sorts them before plotting them.
Could anyone tell me how to plot a quadratic line in the second plot.
You can order it first
i<-order(factor1)
plot(factor1[i],fitted(lm(data1[i]~factor1[i]+I(factor1[i]^2))), type="l")

obtaining regression coefficients in R [duplicate]

This question already has answers here:
Extract regression coefficient values
(4 answers)
Closed 6 years ago.
I have plotted the scatter plot below using the following scripts but still need to obtain the regression coefficient.
Any help would be very much appreciated.
lm.irt12 <- lm(prtemp ~ irt12,data=apirt)
summary(lm.irt12)
plot(apirt$irt12[apirt$surv==1],
apirt$prtemp[apirt$surv==1],
xlab="ave. base of ears (°C)",
ylab="rectal (°C)",
xlim=c(26,42),
ylim=c(30,42),
col='blue')
points(apirt$irt12[apirt$surv==0], apirt$prtemp[apirt$surv==0],col='red')
abline(lm.irt12)
abline(h=36,v=0, col="gray10",lty=20)
#col = "gray60" OR col = "lightgray", lty=3; #(lty=3: broken line, 1:continuous)
text(26.7,36.7,parse(text='36*degree'),col='gray10')
abline(h=34,v=0, col="gray10",lty=20)
#col = "gray60" OR col = "lightgray", lty=3; #(lty=3: broken line, 1:continuous)
text(26.7,34.7,parse(text='34.0*degree'),col='gray10')
You just have to get the coefficient parameter of your linear model object lm.irt12:
lm.irt12$coefficients
To see the parameter of R objects, you can do:
str(lm.irt12)

How to plot a maths function as a heatmap [duplicate]

This question already has answers here:
3D equivalent of the curve function in R?
(4 answers)
Closed 8 years ago.
If x and y can vary from 0 to 10, how can I plot a maths function z = x*(y^2) as an image plot or heatmap ? The x and y should come to their respective axes and z value should be shown as colour on the plot. I could find method to plot only discrete values, not a continuous function. Thanks for your help.
For example
library(emdbook)
curve3d(x*y^2,xlim=c(0,10),ylim=c(0,10),sys3d="image")
curve3d is just a wrapper (the sys3d argument gives you a range of plotting possibilities) -- more generally, you can create a function and use outer() (if it's vectorized) to create a matrix, e.g.
xvec <- yvec <- seq(0,10,length.out=41)
z <- outer(xvec,yvec,function(x,y) x*y^2)
image(xvec,yvec,z)

Resources