Retrieving Regression Coefficient Values

I have a regression model for some time series data that studies drug use. The goal is to fit the spline to the time series and develop 95% CI, etc. The model is as follows:

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) 

The final conclusion 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 

I use the value Pr(>|t|) a2 to check if the data being studied is autocorrelated.

Is it possible to extract this Pr(>|t|) value Pr(>|t|) (in this model 0.33329) and store it in a scalar to perform a logical test?

Alternatively, could it be developed using another method?

+43
r regression lm
Jul 05 2018-11-11T00:
source share
3 answers

Object A summary.lm stores these values ​​in a matrix called 'coefficients' . This way you can access the following value:

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

Or, more generally / readably, coef(summary(mg))["a2","Pr(>|t|)"] . See here why this method is preferred.

+52
Jul 05 2018-11-11T00:
source share

This is where the broom package comes in handy (it uses the neat format).

tidy(mg) will give a well-formed data file with coefficients, t-statistics, etc. Works for other models (e.g. plm, ...).

Example from broom github repo:

 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 
+17
Apr 07 '15 at 9:37
source share

Just pass your regression model to the following function:

  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) } 

Use the following:

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

enter image description here

-one
Feb 19 '17 at 5:39 on
source share



All Articles