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")
}
)
Related
I'm trying to figure out how I can arrange diagnostic plots differently using the plot() function. Here is my code:
mtcars_linear_model <- lm(mpg ~ wt, mtcars)
plot(mtcars_linear_model)
It will print these four plots in my console.
Is there a way to arrange them using ggarrange? I can't seem to print one of them at a time. I thought maybe I could call the index with the plot() function to get the plots one at a time but it doesn't work:
plot(mtcars_linear_model)[1]
I want to use each plot separately in ggarrange like this:
ggarrange(residuals_vs_fitted, normal_qq, scale_location, residuals_vs_leverage)
So that I could get one image with a 2x2 grid of these four diagnostic plots.
Using R base
x11()
par(mfrow=c(2,2))
plot(mtcars_linear_model)
This will produce:
You can reset plot params by par(mfrow=c(1,1))
I have a small multiple plot that looks like this:
The plot presents the results from two models: mpg predicted by cyl and disp for two transmission types. 0 is the first model, fit for automatic transmission. 1 is the second model, fit for manual transmission. The code to get the plot is this:
library(tidyverse)
library(dotwhisker)
mod_mtcars='mpg~cyl+disp'
np_mtcars=mtcars%>%
group_by(am)%>%
do(broom::tidy(lm(mod_mtcars, data= . )))%>%
rename(model=am)
small_multiple(np_mtcars)
I would like to add a horizontal line to each subplot which corresponds to the coefficients of a model fit without groups (a complete pooling model: cp=lm(mpg~cyl+disp, data=mtcars)). I know how to add a generic horizontal line, with intercept 0, for instance. However, does anyone know how to add a different line to each subplot?
When I vectorise the coefficients of cp (cp_coeff=coef(cp)) and add them to the plot, I get all of them at once on every subplot. When I run the below loop, I get the last element of the vector printed on each subplot.
for (i in 1:2){
small_multiple(np_mtcars)+
geom_hline(cp_coeff[i])}
You need to add another layer as follows:
small_multiple(np_mtcars) +
geom_hline(data = broom::tidy(cp)[-1,], aes(yintercept=estimate))
Look at broom::tidy(cp) for an explanation as to why this works (compared to np_mtcars), and think about how it will be plotted given the facets already defined in the graph.
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.
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'm simply trying to display the fit I've generated using lm(), but the lines function is giving me a weird result in which there are multiple lines coming out of one point.
Here is my code:
library(ISLR)
data(Wage)
lm.mod<-lm(wage~poly(age, 4), data=Wage)
Wage$lm.fit<-predict(lm.mod, Wage)
plot(Wage$age, Wage$wage)
lines(Wage$age, Wage$lm.fit, col="blue")
I've tried resetting my plot with dev.off(), but I've had no luck. I'm using rStudio. FWIW, the line shows up perfectly fine if I make the regression linear only, but as soon as I make it quadratic or higher (using I(age^2) or poly()), I get a weird graph. Also, the points() function works fine with poly().
Thanks for the help.
Because you forgot to order the points by age first, the lines are going to random ages. This is happening for the linear regression too; he reason it works for lines is because traveling along any set of points along a line...stays on the line!
plot(Wage$age, Wage$wage)
lines(sort(Wage$age), Wage$lm.fit[order(Wage$age)], col = 'blue')
Consider increasing the line width for a better view:
lines(sort(Wage$age), Wage$lm.fit[order(Wage$age)], col = 'blue', lwd = 3)
Just to add another more general tip on plotting model predictions:
An often used strategy is to create a new data set (e.g. newdat) which contains a sequence of values for your predictor variables across a range of possible values. Then use this data to show your predicted values. In this data set, you have a good spread of predictor variable values, but this may not always be the case. With the new data set, you can ensure that your line represents evenly distributed values across the variable's range:
Example
newdat <- data.frame(age=seq(min(Wage$age), max(Wage$age),length=1000))
newdat$pred <- predict(lm.mod, newdata=newdat)
plot(Wage$age, Wage$wage, col=8, ylab="Wage", xlab="Age")
lines(newdat$age, newdat$pred, col="blue", lwd=2)