Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a regression model for some time series data investigating drug utilisation. The purpose is to fit a spline to a time series and work out 95% CI etc. The model goes 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 summary output of mg is:

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 am using the Pr(>|t|) value of a2 to test if the data under investigation are autocorrelated.

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

Alternatively, can it be worked out using another method?

.@John - Why did you use Pr(>|t|) value of a2 and not any one of the first three columns? – Chetan Arvind Patil Aug 4, 2017 at 21:33

A summary.lm object stores these values in a matrix called 'coefficients'. So the value you are after can be accessed with:

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

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

You mean e.g. coef(summary(mg))[, c("t value","Pr(>|t|)")]? But thanks for the Link - I always forget that! – Christoph Aug 23, 2018 at 9:43 @PatrickT thx. These little things are what make a big difference. I found out about data.table and that transformed my outlook on R .. but still need these kinds of tips. – WestCoastProjects Dec 19, 2018 at 14:34

The package broom comes in handy here (it uses the "tidy" format).

tidy(mg) will give a nicely formated data.frame with coefficients, t statistics etc. Works also for other models (e.g. plm, ...).

Example from broom's 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
                To answer the OP from this: td[1, "estimate"] or td[td$term == "(Intercept)","estimate"] or even tdt <- as.data.table(td); setkey(tdt); tdt["(Intercept)","estimate"]
– PatrickT
                Sep 26, 2017 at 20:46

Just pass your regression model into 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 as follows:

model <- lm(Petal.Width ~ ., data = iris)
plot_coeffs(model)
                I've plundered your code and find it to be useful.  You may want to alter it so that it removes na values and it allows for the main title to be entered from the function.
– DarrenRhodes
                Nov 12, 2019 at 10:48

To answer your question, you can explore the contents of the model's output by saving the model as a variable and clicking on it in the environment window. You can then click around to see what it contains and what is stored where.

Another way is to type yourmodelname$ and select the components of the model one by one to see what each contains. When you get to yourmodelname$coefficients, you will see all of beta-, p, and t- values you desire.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.