Domanda

I'm getting the following error when I run the plot code: Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ. I have many NAs in my data set which I understand is causing the problem. Any ideas as to:

  1. how to deal with NAs in R, and
  2. how to make sure I can plot plots of variables with differing lengths?

I looked at some similar questions posted here, but unfortunately couldn't make sense of it. Needless to say, I'm new to R.

 df <- read.dta("r12.dta")
 attach(df)
 model1 <- lm(rent~I(income^2)+income*races)
 fitted(model1)
 layout(matrix(1:4,2,2))
 plot(model1)
 plot(income, fitted(model1), xlab="Income", ylab="Rent",
      main="Fitted Values for Black  Rent",type="l")
È stato utile?

Soluzione

The lm object contains the variables (with NAs removed) as a dataframe in an element called model. So you can extract the relevant income variable from there to use in your plot:

plot(model1$model$income, fitted(model1), xlab="Income", ylab="Rent",
  main="Fitted Values for Black  Rent", type="l")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top