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 NA
s in my data set which I understand is causing the problem. Any ideas as to:
当我运行绘图代码时出现以下错误:xy.coords(x,y,xlabel,ylabel,log)中的错误:'x'和'y'长度不同。我的数据集中有很多NA,据我所知是导致问题的原因。任何想法:
- how to deal with NAs in R, and
- 如何处理R中的NAs
- 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.
我查看了这里发布的一些类似问题,但遗憾的是无法理解它。不用说,我是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")
1 个解决方案
#1
1
The lm
object contains the variables (with NA
s removed) as a dataframe in an element called model
. So you can extract the relevant income
variable from there to use in your plot:
lm对象包含变量(删除了NAs)作为名为model的元素中的数据帧。因此,您可以从那里提取相关的收入变量,以便在您的情节中使用:
plot(model1$model$income, fitted(model1), xlab="Income", ylab="Rent",
main="Fitted Values for Black Rent", type="l")
#1
1
The lm
object contains the variables (with NA
s removed) as a dataframe in an element called model
. So you can extract the relevant income
variable from there to use in your plot:
lm对象包含变量(删除了NAs)作为名为model的元素中的数据帧。因此,您可以从那里提取相关的收入变量,以便在您的情节中使用:
plot(model1$model$income, fitted(model1), xlab="Income", ylab="Rent",
main="Fitted Values for Black Rent", type="l")