2015-10-06 60 views
0

下面是我的數據框中用於此繪圖的數據的子集;ggplot印刷迴歸方程,其中在一個繪圖上有多個lm

Year region gear Species.Code query LPUE 
1974 Cyprus creel LOB    Pre  0.31 
1975 Cyprus creel LOB    Pre  0.26 
1976 Cyprus creel LOB    Pre  0.33 
1977 Cyprus creel LOB    Pre  0.17 
1978 Cyprus creel LOB    Pre  0.2 
1979 Cyprus creel LOB    Pre  0.22 
1980 Cyprus creel LOB    Pre  0.38 
1981 Cyprus creel LOB    Pre  0.51 
1982 Cyprus creel LOB    Pre  0.57 
1983 Cyprus creel LOB    Pre  0.45 
1984 Cyprus creel LOB    Post 0.43 
1985 Cyprus creel LOB    Post 0.33 
1986 Cyprus creel LOB    Post 0.21 
1987 Cyprus creel LOB    Post 0.69 
1988 Cyprus creel LOB    Post 0.65 
1989 Cyprus creel LOB    Post 0.37 
1990 Cyprus creel LOB    Post 0.35 
1991 Cyprus creel LOB    Post 0.15 
1992 Cyprus creel LOB    Post 0.21 
1993 Cyprus creel LOB    Post 0.17 

我已生成的時間序列數據的線圖和裝配兩個線性迴歸一個用於數據高達1984,一個用於數據1984 使用以下代碼之後;

ggplot(subset(A7,region=="Cyprus"&gear=="creel"&Species.Code=="LOB"),aes(x=Year,y=LPUE,shape=query))+ 
    geom_line()+ 
    geom_smooth(method="lm")+ 
    theme(panel.background = element_rect(fill = 'white', colour = 'black')) 

我想先知道我可以打印在圖上方程(其他例子我已經在堆棧溢出只發現了一個LM處理),其次我怎麼可以保存LM模型,這樣我可以對它們之間的重要性進行統計檢驗。

+0

你擁有的屏幕截圖會有所幫助,因爲你可能沒有包含足夠的數據來重現你擁有的內容。 –

+0

如果你想做統計測試,我建議適合ggplot以外的模型。 – Roland

+0

對不起@MikeWise我不熟悉如何添加屏幕截圖。我只能看到如何從互聯網上添加圖片。 –

回答

2

可以通過堆疊geom_smooth命令添加幾個迴歸線到ggplot。我建議爲你想要的兩個模型創建單獨的數據框。

data_upto84 <- subset(A7, year<1985) 
data_from84 <- subset(A7, year>1984) 

ggplot(data_upto84, aes(YEAR, LPUE)) + 
stat_summary(fun.data=mean_cl_normal) + 
geom_smooth(method = 'lm') + 
geom_smooth(data = data_from84, aes(YEAR, LPUE), method = 'lm') 

關於你的第二個問題:「怎麼其次,我可以保存LM模型,這樣我可以做他們之間的顯着性的統計檢驗」。 您可以通過將其分配給對象保存模型:

model1 <- lm(LPUE ~ 1 + YEAR, data = data_upto84) 
model2 <- lm(LPUE ~ 1 + YEAR, data = data_from84) 

你想用這些模型來測試什麼目前尚不清楚給我。在兩個不同的樣品上運行模型時,標準模型比較(例如使用anova函數)將無效。

相關問題