2011-03-21 44 views
0

任何想法,爲什麼我得到一個錯誤與下面的代碼?R問題。 'car'庫中的'scatterplot'函數。 「對象不是矩陣」錯誤

在此先感謝!

library(lattice) 
library(car) 

set.seed(1234) 
response=round(rnorm(60),2) 
explanatory<-round(c(rnorm(20, 10,1), rnorm(10, 8, 1), rnorm(10,20,1), rnorm(20,18,1)),2) 
gender=c(rep("male", 30), rep("female", 30)) 
factor1=as.factor(c(rep('a', 20), rep('b', 20), rep('c', 20))) 
scatterplot(response~explanatory, smooth=FALSE, reg.line=FALSE) 

回答

1

我覺得scatterplot的目的是與數據幀使用,請檢查:

df <- data.frame(response, explanatory) 
scatterplot(response~explanatory, df, smooth=FALSE, reg.line=FALSE) 

輸出:

result image with scatterplot

我不知道你想性別因子1做。

+0

賓果!我在數據框中使用了這些向量,但是我沒有使用數據幀作爲scatterplot()函數的參數。我打算根據'性別'和'factor1'進行繪圖 – Michael 2011-03-21 23:58:27

+0

儘管此代碼運行。非常抱歉格式化。 '需要(Sleuth2) 要求(MASS) 要求(校準) 要求(格子) 庫(車) 連接(ex0918) Wing.Size <-c(女性,男性) 性別< -as.factor(C(REP( 「女性」,長度(女性)),代表( 「男性」,長度(男性)))) 大陸<-as.factor(C(代表(C(REP(」 NA」,11),代表( 「EU」,10) \t \t \t \t \t \t \t),2 \t \t \t \t \t \t \t) \t \t \t \t \t) \t \t \t \t \t) latitude2 <-c(REP(緯度,2)) Gender.continent = as.factor(膏(性別,大陸)) 類型<-as.numeric(Gender.continent) \t \t \t \t \t scatterplot(Wing.Size〜latitude2 | Gender.continent,smooth = FALSE,reg.line = FALSE)' – Michael 2011-03-22 00:00:51

1
scatterplot(cbind(response, explanatory), smooth=FALSE, reg.line=FALSE) 

難道這就是你要找的人?錯誤在說,它不是一個矩陣,因爲你提供的不是。

enter image description here

貌似性別和因子1無關您的問題。不知道我理解爲什麼代碼被列入

+0

我的目標最終是根據「性別」和我已經通過粘貼這兩個因素作爲新的變量(gender.factor1)以前做「因子1」,然後「散點圖(響應〜說明繪製那些| gender.factor1 ,smooth = FALSE,reg.line = FALSE)'這似乎並沒有在這個代碼上工作,但我不知道爲什麼。 – Michael 2011-03-21 23:46:55