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.
Related
Had a look around and couldn't find an answer to my question, so finally stopped lurking. I've been creating multiple scatter plots comparing each column to the others as shown here
I used the script
attach(`File`)
plot(`Files`[,c(2,3,4,5,6,7,8)])
However I can't seem to correctly input the command to annotate the regression line and r2 value onto the graphs.
here is the solution. Assume Z is your design matrix.
z=matrix(rnorm(500),ncol=5)
pairs( z, panel=function(x,y){
points(x,y)
abline(lm(y~x), col='red')
text(0,1.5,labels = paste('R2=',round((cor(x,y))^2,2)) ,col='red' )
})
and result should be like this
I am trying to have output 2 different graphs with a regression line. I am using the mtcars data set which I believe you can load into R. So, I am comparing 2 different pairs of information to create a regression line. And the problem seems to be that the 2nd regression line from the 2nd graph is for some reason in the first graph as well.
I just want it to show 1 regression line in each graph the way it should be.
mtcars
names(mtcars)
attach(mtcars)
par(mfrow=c(1,2), bg="white")
with(mtcars,
{
regrline=(lm(gear~mpg))
abline(regrline)
plot(mpg,gear,abline(regrline, col="red"),main="MPG vs Gear")
# The black line in the first graph is the regression line(blue) from the second graph
regrline=(lm(cyl~disp))
abline(regrline)
plot(disp,cyl,abline(regrline, col="blue"),main="Displacement vs Number of Cylinder")
})
Also when I run the code separately for plotting, I don't see the black line. Its only when I run it with the: with() it causes a problem.
First of all, you really should avoid using attach. And for functions that have data= parameters (like plot and lm), its usually wiser to use that parameter rather than with().
Also, abline() is a function that should be called after plot(). Putting it is a parameter to plot() doesn't really make any sense.
Here's a better arrangement of your code
par(mfrow=c(1,2), bg="white")
regrline=lm(gear~mpg, mtcars)
plot(gear~mpg,mtcars,main="MPG vs Gear")
abline(regrline, col="red")
regrline=lm(cyl~disp, mtcars)
plot(cyl~disp,mtcars,main="Displacement vs Number of Cylinder")
abline(regrline, col="blue")
You got that second regression line because you were calling abline() before plot() for the second regression, do the line drew on the first plot.
Here is your code cleaned up a little. You were making redundant calls to abline that was drawing the extra lines.
By the way, you don't need to use attach when you use with. with is basically a temporary attach.
par(mfrow=c(1,2), bg="white")
with(mtcars,
{
regrline=(lm(gear~mpg))
plot(mpg,gear,main="MPG vs Gear")
abline(regrline, col="red")
regrline=(lm(cyl~disp))
plot(disp,cyl,main="Displacement vs Number of Cylinder")
abline(regrline, col="blue")
}
)
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
I would like to draw a straight line on plot using the following linear equation.
y = 2.522x-1.331
I used the following code to get a scatterplot.
data=read.csv("C://book.csv")
plot(data$x,data$y)
You need to use function abline:
abline(a=-1.331, b=2.522)
Argument a is the intercept and argument b the slope.
See ?abline for more details.
Use abline, e.g.
abline(-1.331, 2.522)
I am doing a survival analysis and have produced the survival graph using
plot function to plot the Kaplan-Meier(KA variable) estimate as y value against time.
lines function to plot the step lines between estimate i and i+1, for each i=1,2,....
The code is as follows:
plot(KA)
for( i in 1:(length(KA)-1)){
lines(c(i,i+1),c(KA[i],KA[i])) # The horizontal step lines
lines(c(i+1,i+1),c(KA[i],KA[i+1])) # The vertical step lines
}
Now I want to make a more beautiful survival graph using ggplot2 package.
The question is: how to add the step lines into the graph?
I'm sorry, I can not put graphs as my reputation is less than 10.
Have a look at either geom_step, geom_path or geom_segment.
They might be helpful in what you are trying to achieve.
http://had.co.nz/ggplot2/geom_step.html
http://had.co.nz/ggplot2/geom_path.html
http://had.co.nz/ggplot2/geom_segment.html