🔒 Closed R-Studio error program?

Status
Not open for further replies.

Lunathicc

Forum Guru
ano po kaya error mga lods okay naman sa pag process ko ng code pero pag dating dito nagka error, baka may nakaka alam, maraming salamat agad. R language po yan
331935359_602183907995970_4066308354931097376_n.png

Code:
#Extract elements
res1 <- resid(model1)
fit1 <- fitted(model1)
res2 <- resid(model2)
fit2 <- fitted(model2)

#Calculate axis range
resRange <- c(-1, 1) * max(abs(res1), abs(res2))
fitRange <- range(fit1, fit2)

#Create plot for  model1> add points for model2
plot(fit1, res1, xlim = fitRange, ylim = resRange, col = "red", pch = 16, main = "Residuals vs Fitted Values", xlab = "Fitted Values", ylab = "Residuals")
points(fit2, res2, col = "blue", pch = 16)

#Add reference and smooth lines
abline(h = 0, lty = 2)
lines(loess.smooth(fit1, res1), col = "red")
lines(loess.smooth(fit2, res2), col = "blue")
legend("bottom left", c("mpg ~ wt", "mpg ~ wt + hp"), fill = c("red", "blue"))
 
Based on the error message in the screenshot you provided, it looks like the issue is with the legend function at the end of your code. Specifically, the error is caused because the fill parameter is being used, but it is not a valid argument for the legend function in R.

One common way to specify colors in the legend function is to use the col argument instead of fill. Here is an updated version of your code that should work correctly:

Code:
#Extract elements
res1 <- resid(model1)
fit1 <- fitted(model1)
res2 <- resid(model2)
fit2 <- fitted(model2)

#Calculate axis range
resRange <- c(-1, 1) * max(abs(res1), abs(res2))
fitRange <- range(fit1, fit2)

#Create plot for  model1> add points for model2
plot(fit1, res1, xlim = fitRange, ylim = resRange, col = "red", pch = 16, main = "Residuals vs Fitted Values", xlab = "Fitted Values", ylab = "Residuals")
points(fit2, res2, col = "blue", pch = 16)

#Add reference and smooth lines
abline(h = 0, lty = 2)
lines(loess.smooth(fit1, res1), col = "red")
lines(loess.smooth(fit2, res2), col = "blue")
legend("bottom left", c("mpg ~ wt", "mpg ~ wt + hp"), col = c("red", "blue"), pch = 16)

In this updated code, I simply changed fill = c("red", "blue") to col = c("red", "blue") in the legend function, and added the pch = 16 parameter to specify the point symbol for the legend entries.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 382
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
985
Guests online
916
Total visitors
1,901

Forum statistics

Threads
2,277,922
Posts
28,979,805
Members
1,228,172
Latest member
gdarpawijaya
Back
Top