obtaining regression coefficients in R [duplicate] - r

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)

Related

Normal density curve [duplicate]

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")

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).

Shading a region under a PDF [duplicate]

This question already has answers here:
Shading a kernel density plot between two points.
(5 answers)
Closed 5 years ago.
I've used the following to plot a Weibull distribution W(2,3):
Weibull=function(x){
(2*x/9)*exp(-(x^2)/9)
}
xx=seq(from=0,to=10,length.out=1000)
xx1=Weibull(xx)
plot(xx,xx1,type="l")
polygon(xx, xx1, col = "red", border = NA)
PDF plot
I want to shade the region between (4,10) only. How would I go about doing this?
xx2=seq(from=4,to=10,length.out=1000)
yy2=c(Weibull(xx2),0)
xx2=c(xx2,4)
plot(xx,xx1,type="l")
polygon(xx2, yy2, col = "red", border = NA)

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")

Add exponential curve to plot [duplicate]

This question already has answers here:
Exponential regression in R
(2 answers)
Closed 6 years ago.
I am looking to add a curve to my plot to show an exponential decrease over time. Ive plotted two small sets of data with the basic plot() function and just for clarity I wanted to add a smoothed line.
The data points for the two datasets are
1.00000 0.37360 0.27688 0.22992 0.17512 0.13768 0.08048
1.00000000 0.44283122 0.30871143 0.23647913 0.22586207 0.09800363 0.06206897
with the x values showing the decay over time (0,1,2,3,4,5,6)
I like to use ggplot2 as it makes adding lines from fitted models so simple.
Without to much to go on the following may help you out....
#prepare the data
y <- c(1.00000, 0.37360, 0.27688, 0.22992, 0.17512, 0.13768, 0.08048,
1.00000000, 0.44283122, 0.30871143, 0.23647913, 0.22586207, 0.09800363, 0.06206897)
x <- c(0,1,2,3,4,5,6,0,1,2,3,4,5,6)
z <- c(1,1,1,1,1,1,1,0,0,0,0,0,0,0)
dat <- as.data.frame(cbind(x,y,z))
#load the library
library(ggplot2)
#plot the data
ggplot(data=dat,aes(x=x,y=y))+
#add Points with different shapes depending on factor z
geom_point(aes(shape=factor(z)))+
#Add line using non-linear regreassion
stat_smooth(method="nls",formula = y~a*exp(-x*b),method.args=list(start=c(a=2,b=2)),se=F,color="red")+
#add line using linear regression
stat_smooth(method="lm",formula = y~exp(-x),se=F,color="blue")

Resources