Abline won't appear in R - r

I have run regression in R, when trying to fit an abline to the plot nothing happens, no error message appears.
I've had to log transform the data so I'm wondering if this is the issue? The data is not normally distributed however we have still been asked to perform regression.
Here is what I've tried so far:
alldata<-read.csv(file.choose(),header=T)
attach(alldata)
plot(weight.g,wingspan.mm,log="xy")
abline(lm(wingspan.mm~weight.g))
fit1<-lm(wingspan.mm~weight.g)
> summary(fit1)
fit2<-lm(log(wingspan.mm)~log(weight.g))
plot(fit2)
plot(weight.g,wingspan.mm,log="xy")
abline(fit2)
abline(lm(log(wingspan.mm)~log(weight.g)))
Can anyone spot where I am going wrong?
Thanks,
Kate

abline won't plot a regression line over a log-transformed plot.
For instance this will only plot the points and not the regression line
plot(speed~dist, cars, log="xy")
abline(lm(speed~dist, cars))
To get around the problem use the untf parameter
plot(speed~dist, cars, log="xy")
abline(lm(speed~dist, cars), untf=T)
From ?abline:
If untf is true, and one or both axes are log-transformed, then a curve is drawn corresponding to a line in original coordinates, otherwise a line is drawn in the transformed coordinate system

Related

Line not appearing on plot when using lines() in R

I'm doing a polynomial regression and I want to plot it.
My code is the following:
to create polynomial regression to degree 1
mreg6=lm(user_Score~poly(year_of_Release,1))
to create plot
plot(year_of_Release~user_Score, col='gray')
to create line
lines(sort(year_of_Release),predict(mreg6)[order(year_of_Release)],col='red')
to create legend
legend('topright',lty=1:2,col=c('red'),c('degree 1'))
When I run the code, there is no error but the line does not appear on graph.
Do any of you know what I might be doing wrong?
I found my answer: I was putting ~ instead of , in plot() so it was giving me another regression.

How do I plot an abline() when I don't have any data points (in R)

I have to plot a few different simple linear models on a chart, the main point being to comment on them. I have no data for the models. I can't get R to create a plot with appropriate axes, i.e. I can't get the range of the axes correct. I think I'd like my y-axis to 0-400 and x to be 0-50.
Models are:
$$
\widehat y=108+0.20x_1
$$$$
\widehat y=101+2.15x_1
$$$$
\widehat y=132+0.20x_1
$$$$
\widehat y=119+8.15x_1
$$
I know I could possibly do this much more easily in a different software or create a dataset from the model and estimate and plot the model from that but I'd love to know if there is a better way in R.
As #Glen_b noticed, type = "n" in plot produces a plot with nothing on it. As it demands data, you have to provide anything as x - it can be NA, or some data. If you provide actual data, the plot function will figure out the plot margins from the data, otherwise you have to choose the margins by hand using xlim and ylim arguments. Next, you use abline that has parameters a and b for intercept and slope (or h and v if you want just a horizontal or vertical line).
plot(x=NA, type="n", ylim=c(100, 250), xlim=c(0, 50),
xlab=expression(x[1]), ylab=expression(hat(y)))
abline(a=108, b=0.2, col="red")
abline(a=101, b=2.15, col="green")
abline(a=132, b=0.2, col="blue")
abline(a=119, b=8.15, col="orange")

Plot which parameter where in R?

So... I'm looking at an example in a book that goes something like this:
library(daewr)
mod1 <- aov(height ~ time, data=bread)
summary(mod1)
...
par(mfrow=c(2,2))
plot(mod1, which=5)
plot(mod1, which=1)
plot(mod1, which=2)
plot(residuals(mod1) ~ loaf, main="Residuals vs Exp. Units", font.main=1, data=bread)
abline(h = 0, lty = 2)
That all works... but the text is a little vague about the purpose of the parameter 'which='. I dug around in the help (in Rstudio) on plot() and par(), looked around online... found some references to a different 'which()'... but nothing really referring me to the purpose/syntax for the parameter 'which=' inside plot().
A bit later (next page, figures) I found a mention of using names(mod1) to view the list of quantities calculated by aov... which I presume is what which= is refering to, i.e. which item in the list to plot where in the 2x2 matrix of plots. Yay. Now where the heck is that buried in the docs?!?
which selects which plot to be displayed:
A plot of residuals against fitted values
A normal Q-Q plot
A Scale-Location plot of sqrt(| residuals |) against fitted values
A plot of Cook's distances versus row labels
A plot of residuals against leverages
A plot of Cook's distances against leverage/(1-leverage)
By default, the first three and 5 are provided.
Check ?plot.lm in r for more details.

How do I plot the 'inverse' of a survival function?

I am trying to plot the inverse of a survival function, as the data I'm is actually an increase in proportion of an event over time. I can produce Kaplan-Meier survival plots, but I want to produce the 'opposite' of these. I can kind of get what I want using the following fun="cloglog":
plot(survfit(Surv(Days_until_workers,Workers)~Queen_Number+Treatment,data=xdata),
fun="cloglog", lty=c(1:4), lwd=2, ylab="Colonies with Workers",
xlab="Days", las=1, font.lab=2, bty="n")
But I don't understand quite what this has done to the time (i.e. doesn't start at 0 and distance decreases?), and why the survival lines extend above the y axis.
Would really appreciate some help with this!
Cheers
Use fun="event" to get the desired output
fit <- survfit(Surv(time, status) ~ x, data = aml)
par(mfrow=1:2, las=1)
plot(fit, col=2:3)
plot(fit, col=2:3, fun="event")
The reason for fun="cloglog" screwing up the axes is that it does not plot a fraction at all. It is instead plotting this according to ?plot.survfit:
"cloglog" creates a complimentary log-log survival plot (f(y) = log(-log(y)) along with log scale for the x-axis)
Moreover, the fun argument is not limited to predefined functions like "event" or "cloglog", so you can easily give it your own custom function.
plot(fit, col=2:3, fun=function(y) 3*sqrt(1-y))

Only plotting the fitted spline line and not the data points

I have checked my references, it seems to me that to fit a dataset with x and y, many tutorial need to first plot the x and y, then the fitted line is plot. The normal procedure is like below:
## Calculate the fitted line
smoothingSpline = smooth.spline(tree_number[2:100], jaccard[1:99], spar=0.35)
plot(tree_number[2:100],jaccard[1:99]) #plot the data points
lines(smoothingSpline) # add the fitted spline line.
However, I do not want to plot the tree_number and jaccard, but rather, I only want to plot the fitted spline line in the plot, how should I do?
You can use the associcated plot function:
plot(smoothingSpline, type="l")
Or you can extract the x and y values explicitly and plot them
plot(smoothingSpline$x, smoothingSpline$y, type="l")
Why not just plot(smoothingSpline, type = "l")? That should allow you to add the fitted spline line without having to first plot the data points.

Resources