2017-10-10 388 views
0

我有一個大的數據集,我正在進行許多回歸分析。我正在使用r的lmodel2包減少主軸迴歸。我需要做的是從RMA模型中提取回歸係數(r平方,p值,斜率和截距)。我能做到這一點很容易足以與使用OLS迴歸:使用lmodel2包獲得減少主軸迴歸模型的迴歸係數

RSQ<-summary(model)$r.squared 
PVAL<-summary(model)$coefficients[2,4] 
INT<-summary(model)$coefficients[1,1] 
SLOPE<-summary(model)$coefficients[2,1] 

,然後導出到.csv

export<-data.frame(RSQ,PVAL,INT,SLOPE) 
write.csv(export, file="FILE_NAME.csv",row.names=F) 

這些命令似乎並不爲lmodel2迴歸工作。有人知道該怎麼做嗎?

下面是數據的一個小樣本:

x   y 
0.440895993 227.7 
0.294277869 296.85 
0.171754892 298.05 
0   427.65 
0.210884179 215.55 
0.053238011 293.7 
0.105395366 127.9 
0.463933834 229.5 
0   165.45 
0.482128605 192.15 
0.247341039 266.9 
0   349.35 
0.198833301 185.05 
0.170786027 203.85 
0.269818315 207.05 
0.129543682 222.75 
0.441665334 251.35 
0   262.8 
0.517974685 107.05 
0.446336968 191.6 

而且我使用

library(lmodel2) 
data<-sample_data 
mod_2<-lmodel2(y~x,data=data,"interval","interval",99) 
mod_2 

回答

0

什麼這個模型II迴歸代碼?

# making data reproducable 
data <- read.table(text = "x   y 
0.440895993 227.7 
0.294277869 296.85 
0.171754892 298.05 
0   427.65 
0.210884179 215.55 
0.053238011 293.7 
0.105395366 127.9 
0.463933834 229.5 
0   165.45 
0.482128605 192.15 
0.247341039 266.9 
0   349.35 
0.198833301 185.05 
0.170786027 203.85 
0.269818315 207.05 
0.129543682 222.75 
0.441665334 251.35 
0   262.8 
0.517974685 107.05 
0.446336968 191.6", header = TRUE) 

#estimate model 
library(lmodel2) 
mod_2 <- lmodel2(y ~ x, data = data, "interval", "interval", 99) # 99% ci 

拿在summary()簡單的介紹一下,這是信息的統計模型如何保存。 (你也可以嘗試str())。

# view summary 
summary(mod_2) 
#      Length Class  Mode 
# y     20  -none-  numeric 
# x     20  -none-  numeric 
# regression.results 5  data.frame list 
# confidence.intervals 5  data.frame list 
# eigenvalues   2  -none-  numeric 
# H      1  -none-  numeric 
# n      1  -none-  numeric 
# r      1  -none-  numeric 
# rsquare    1  -none-  numeric 
# P.param    1  -none-  numeric 
# theta     1  -none-  numeric 
# nperm     1  -none-  numeric 
# epsilon    1  -none-  numeric 
# info.slope   1  -none-  numeric 
# info.CI    1  -none-  numeric 
# call     6  -none-  call 

可以看出該GOFS的變量名(你叫他們「命令」)所特有的包。您可以在$運算符之後將它們添加到模型的對象名稱中進行選擇。

# Getting r squared 
(RSQ <- mod_2$rsquare) 
# [1] 0.1855163 

對於lmodel2希望$regression.results係數和他們的統計數據。

mod_2$regression.results 
# Method Intercept  Slope Angle (degrees) P-perm (1-tailed) 
# 1 OLS 277.2264 -177.0317  -89.67636    0.04 
# 2  MA 457.7304 -954.2606  -89.93996    0.04 
# 3 SMA 331.5673 -411.0173  -89.86060    NA 
# 4 RMA 296.6245 -260.5577  -89.78010    0.04 

# wanted results from the RMA model 
(INT <- mod_2$regression.results[[2]][4]) 
# [1] 296.6245 
(SLOPE <- mod_2$regression.results[[3]][4]) 
# [1] -260.5577 
(PVAL <- mod_2$regression.results[[5]][4]) 
# [1] 0.04 

# Combined together in a data frame: 
data.frame(RMA = rbind(INT, SLOPE, PVAL)) 
#    RMA 
# INT 296.6245 
# SLOPE -260.5577 
# PVAL  0.0400 
+0

感謝您的快速和啓發回覆。儘管'mod_2 $ x [c(2,4)]'給了我 '[1] 0.2942779 0.0000000',我仍然有點困惑。 RMA斜率和截距值分別爲-260.5577和296.6245。我錯過了什麼? –

+0

不客氣。我只是擴大了我的答案,現在可以爲你工作嗎? – jaySf