lm - In R linear model, get p-values for only the interaction coefficients -


if have summary table linear model in r, how can p-values associated interaction estimates, or group intercepts, etc., without having count row numbers?

for example, model such lm(y ~ x + group) x continuous , group categorical, summary table lm object has estimates for:

  1. an intercept
  2. x, slope across groups
  3. 5 within group differences overall intercept
  4. 5 within group differences overall slope.

i figure out way each of these group of p-values, if number of groups or model formula change. maybe there information summary table somehow uses group rows?

following example data set 2 different models. first model has 4 different sets of p-values might want separately, while second model has 2 sets of p-values.

x <- 1:100 groupa <- .5*x + 10 + rnorm(length(x), 0, 1) groupb <- .5*x + 20 + rnorm(length(x), 0, 1) groupc <- .5*x + 30 + rnorm(length(x), 0, 1) groupd <- .5*x + 40 + rnorm(length(x), 0, 1) groupe <- .5*x + 50 + rnorm(length(x), 0, 1) groupf <- .5*x + 60 + rnorm(length(x), 0, 1)  mydata <- data.frame(x = x,     y = c(groupa, groupb, groupc, groupd, groupe, groupf),     group = rep(c("a","b","c","d","e","f"), each = length(x)) )  mymod1 <- lm(y ~ x + group + x:group, data = mydata) mymod2 <- lm(y ~ group + x:group - 1, data = mydata) summary(mymod1) summary(mymod2) 

you can access coefficients , associated statistics via summary()$coefficients, so:

> summary(mymod1)$coefficients                  estimate  std. error      t value      pr(>|t|) (intercept)  9.8598180335 0.207551769  47.50534335 1.882690e-203 x            0.5013049448 0.003568152 140.49427911  0.000000e+00 groupb       9.9833257879 0.293522526  34.01212819 5.343527e-141 groupc      20.0988336744 0.293522526  68.47458673 2.308586e-282 groupd      30.0671851583 0.293522526 102.43569906  0.000000e+00 groupe      39.8366758058 0.293522526 135.71931370  0.000000e+00 groupf      50.4780382104 0.293522526 171.97330259  0.000000e+00 x:groupb    -0.0001115097 0.005046129  -0.02209807  9.823772e-01 x:groupc     0.0004144536 0.005046129   0.08213297  9.345689e-01 x:groupd     0.0022577223 0.005046129   0.44741668  6.547390e-01 x:groupe     0.0024544207 0.005046129   0.48639675  6.268671e-01 x:groupf    -0.0052089956 0.005046129  -1.03227556  3.023674e-01 

of want p-values, i.e. 4th column:

> summary(mymod1)$coefficients[,4]   (intercept)             x        groupb        groupc        groupd        groupe        groupf      x:groupb      x:groupc  1.882690e-203  0.000000e+00 5.343527e-141 2.308586e-282  0.000000e+00  0.000000e+00  0.000000e+00  9.823772e-01  9.345689e-01       x:groupd      x:groupe      x:groupf   6.547390e-01  6.268671e-01  3.023674e-01  

lastly, want p-values of particular coefficients, either intercepts or interaction terms. 1 way of doing match coefficient names (names(summary(mymod1)$coefficients[,4])) regex via grepl() , using logical vector grepl returns index:

> # group dummies > summary(mymod1)$coefficients[grepl('^group[a-f]',names(summary(mymod1)$coefficients[,4])),4]        groupb        groupc        groupd        groupe        groupf  5.343527e-141 2.308586e-282  0.000000e+00  0.000000e+00  0.000000e+00  > # interaction terms > summary(mymod1)$coefficients[grepl('^x:group[a-f]',names(summary(mymod1)$coefficients[,4])),4]  x:groupb  x:groupc  x:groupd  x:groupe  x:groupf  0.9823772 0.9345689 0.6547390 0.6268671 0.3023674  

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -