2011-07-05 67 views
45

我有一些調查藥物利用率的時間序列數據的迴歸模型。其目的是,以適應花的時間序列,並制定出95%CI等模型去如下:提取回歸係數值

id <- ts(1:length(drug$Date)) 
a1 <- ts(drug$Rate) 
a2 <- lag(a1-1) 
tg <- ts.union(a1,id,a2) 
mg <-lm (a1~a2+bs(id,df=df1),data=tg) 

mg摘要輸出是:

Call: 
lm(formula = a1 ~ a2 + bs(id, df = df1), data = tg) 

Residuals: 
    Min  1Q Median  3Q  Max 
-0.31617 -0.11711 -0.02897 0.12330 0.40442 

Coefficients: 
        Estimate Std. Error t value Pr(>|t|)  
(Intercept)  0.77443 0.09011 8.594 1.10e-11 *** 
a2     0.13270 0.13593 0.976 0.33329  
bs(id, df = df1)1 -0.16349 0.23431 -0.698 0.48832  
bs(id, df = df1)2 0.63013 0.19362 3.254 0.00196 ** 
bs(id, df = df1)3 0.33859 0.14399 2.351 0.02238 * 
--- 
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

我現在用的是Pr(>|t|)a2來測試被調查的數據是否是自相關的。

是否可以提取Pr(>|t|)(在此模型中爲0.33329)的此值並將其存儲爲標量以執行邏輯測試?

另外,它可以使用另一種方法嗎?

+0

。@ John - 爲什麼你使用'a2'的Pr(> | t |)'值而不是前三列中的任何一列? –

回答

53

A summary.lm對象將這些值存儲在名爲'coefficients'matrix中。

a2Pval <- summary(mg)$coefficients[2, 4] 

,或者更一般地說/可讀取,coef(summary(mg))["a2","Pr(>|t|)"]:那麼,你是後的值可以被訪問。請參閱here爲什麼此方法是首選。

18

broom在這裏派上用場(它使用「整潔」格式)。

tidy(mg)將給出一個很好的格式化數據幀,包含係數,t統計等。也適用於其他模型(例如,plm,...)。

broom的GitHub庫示例:

lmfit <- lm(mpg ~ wt, mtcars) 
require(broom)  
tidy(lmfit) 

     term estimate std.error statistic p.value 
1 (Intercept) 37.285 1.8776 19.858 8.242e-19 
2   wt -5.344 0.5591 -9.559 1.294e-10 

is.data.frame(tidy(lmfit)) 
[1] TRUE 
+0

要回答這個OP:''td [1,「估計」]''或''td [td $ term ==「(Intercept)」,「估計」]甚至是'tdt < - as .data.table(TD); setkey的(TDT); TDT [ 「(截取)」, 「估計」]'' – PatrickT

0

只是通過迴歸模型分爲以下幾個功能:

plot_coeffs <- function(mlr_model) { 
     coeffs <- coefficients(mlr_model) 
     mp <- barplot(coeffs, col="#3F97D0", xaxt='n', main="Regression Coefficients") 
     lablist <- names(coeffs) 
     text(mp, par("usr")[3], labels = lablist, srt = 45, adj = c(1.1,1.1), xpd = TRUE, cex=0.6) 
    } 

使用方法如下:

model <- lm(Petal.Width ~ ., data = iris) 

plot_coeffs(model) 

enter image description here