2014-02-25 211 views
1

我在運行劇情代碼時出現以下錯誤:Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ。我的數據集中有很多NA s,據我瞭解這是導致問題的原因。任何想法:xy.coords在處理NA時出錯

  1. 如何R中處理NAS和
  2. 如何確保我可以積的變量地塊有不同的長度?

我看了一些類似的問題發佈在這裏,但不幸的是無法理解它。不用說,我是新來的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

lm對象包含變量(用NA小號移除)作爲元素的數據幀稱爲model。因此,您可以從那裏提取相關的income變量,以便在您的圖中使用:

plot(model1$model$income, fitted(model1), xlab="Income", ylab="Rent", 
    main="Fitted Values for Black Rent", type="l") 
+0

感謝您編輯和回覆我的帖子。偉大的建議,再次感謝! – torentino