0

im brand new to coding in R and im having issues with my lm function.

f <- lm(comm_YLL ~ region, data = hw1) # this saves properly
plot(xlab = 'years of life lost', ylab = 'reagression') # this returns the error below

abline(f) # haven't gotten this far

Error in xy.coords(x, y, xlabel, ylabel, log) : argument "x" is missing, with no default

I have no idea what any of this means, and am just generally confused how the lm function works. in this case comm_yll is a years of life lost to whatever, and region is what region a country is in. hw1 is the data im dragging this from

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • Can you provide a reproducible example of your dataset ? (see this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Feb 26 '20 at 04:05
  • I believe the reproducible example would be http://prntscr.com/r7q7xy with a regression line showing positive or negative slope y=a+bx, im still new to stack overflow so i am sorry ahead of time if this isnt what you wanted @dc37 – mephesto Feb 26 '20 at 04:09
  • 2
    That's a picture of a scatterplot, not a set of data we could run your code on. You should read through the link @dc37 posted. You called `plot` without anything to plot, just labels but no data – camille Feb 26 '20 at 04:14
  • I think a reproducable, is this `f <- lm(mpg ~ disp, data = mtcars) plot(mtcars$disp, mtcars$mpg) abline(f)` I was trying to mimick this code with my own data and it created the screenshot up above – mephesto Feb 26 '20 at 04:20
  • Do you still have an error though? Because this plot code has data, whereas the original did not. You can [edit] the post to include additional information – camille Feb 26 '20 at 04:37

2 Answers2

1

As the comments have mentioned above without a reproducible example it is hard to understand the true problem here.
At a guess, your plot function does not call any data. You need to specify a vector for the 'x' variable in the function. Try

plot(x = hw1$comm_YLL, y = hw1$region, xlab = 'years of life lost', ylab = 'reagression') 

Also, note that your lm associates the response variable comm_YLL as a y axis variable on a plot and the predictor region as an x axis variable. So perhaps you want.

plot(x = hw1$region, y = hw1$comm_YLL, xlab = 'reagression', ylab = 'years of life lost')
abline(f)

Or

plot(region ~ comm_YLL, ,data = hw1, xlab = 'reagression', ylab = 'years of life lost')
abline(f)
0

Data from your comment:

hw1 <- data.frame(comm_YLL = c(17, 50, 84, 21, 12, 18),
                  region = c(5, 1, 1, 2, 4, 2))

Assume that

  • Predictor variable(X) : region
  • Response variable(Y) : comm_YLL

Simple Linear Model

mod <- lm(comm_YLL ~ region, data = hw1)

Plot

Actually plot is a generic function that it calls other functions according to what you input. There are 2 ways to plot X, Y, and regression line.

1. Input a formula object

In this way, plot call plot.formula to do the plotting job and you need to set data = hw1 to tell this function region and comm_YLL come from hw1.

plot(comm_YLL ~ region, data = hw1)
abline(mod, col = 2)

2. Input 2 vectors separately

plot call plot.default and you need to use $ symbol extracting values in hw1.

plot(hw1$region, hw1$comm_YLL)
abline(mod, col = 2)

If you just want the regression line, set type = "n" in plot.

plot(comm_YLL ~ region, hw1, type = "n")
abline(mod, col = 2)

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51