Linear Regression and storing results in data frame [duplicate]

有些话、适合烂在心里 提交于 2019-11-28 08:44:01

Here's a vote for the plyr package and ddply().

plyrFunc <- function(x){
  mod <- lm(b~c, data = x)
  return(summary(mod)$coefficients[2,3])
  }

tStats <- ddply(dF, .(a), plyrFunc)
tStats
  a         V1
1 a  1.6124515
2 b -0.1369306
3 c  0.6852483

Here is a solution using dplyr and tidy() from the broom package. tidy() converts various statistical model outputs (e.g. lm, glm, anova, etc.) into a tidy data frame.

library(broom)
library(dplyr)

data <- data_frame(a, b, c)

data %>% 
  group_by(a) %>% 
  do(tidy(lm(b ~ c, data = .))) %>% 
  select(variable = a, t_stat = statistic) %>% 
  slice(2)

#   variable     t_stat
# 1        a  1.6124515
# 2        b -0.1369306
# 3        c  0.8000000  

Or extracting both, the t-statistic for the intercept and the slope term:

data %>% 
  group_by(a) %>% 
  do(tidy(lm(b ~ c, data = .))) %>% 
  select(variable = a, term, t_stat = statistic)

#   variable        term     t_stat
# 1        a (Intercept)  1.2366939
# 2        a           c  1.6124515
# 3        b (Intercept)  2.6325081
# 4        b           c -0.1369306
# 5        c (Intercept)  1.4572335
# 6        c           c  0.8000000

You can use the lmList function from the nlme package to apply lm to subsets of data:

# the data
df <- data.frame(a, b, c)

library(nlme)
res <- lmList(b ~ c | a, df, pool = FALSE)
coef(summary(res))

The output:

, , (Intercept)

   Estimate Std. Error  t value   Pr(>|t|)
a 0.1000000 0.08086075 1.236694 0.30418942
b 0.2304348 0.08753431 2.632508 0.07815663
c 0.1461538 0.10029542 1.457233 0.24110393

, , c

     Estimate Std. Error    t value  Pr(>|t|)
a  0.50000000  0.3100868  1.6124515 0.2052590
b -0.04347826  0.3175203 -0.1369306 0.8997586
c  0.15384615  0.1923077  0.8000000 0.4821990

If you want the t values only, you can use this command:

coef(summary(res))[, "t value", -1]
#          a          b          c 
#  1.6124515 -0.1369306  0.8000000  

Use split to subset the data and do the looping by lapply

dat <- data.frame(b,c)
dat_split <- split(x = dat, f = a)
res <- sapply(dat_split, function(x){
  summary(lm(b~c, data = x))$coefficients[2,3]
})

Reshape the result to your needs:

data.frame(variable = names(res), "t-stat" = res) 

  variable     t.stat
a        a  1.6124515
b        b -0.1369306
c        c  0.8000000

You could do this:

a<-  c("a","a","a","a","a",
       "b","b","b","b","b",
       "c","c","c","c","c")     
b<-  c(0.1,0.2,0.3,0.2,0.3,
       0.1,0.2,0.3,0.2,0.3,
       0.1,0.2,0.3,0.2,0.3)
c<-  c(0.2,0.1,0.3,0.2,0.4,
       0.2,0.5,0.2,0.1,0.2,
       0.4,0.2,0.4,0.6,0.8)
df <- data.frame(a,b,c)


t.stats <- t(data.frame(lapply(c('a','b','c'), 
             function(x) summary(lm(b~c,data=df[df$a==x,]))$coefficients[2,3])))
colnames(t.stats) <- 't-stat'
rownames(t.stats) <- c('a','b','c')

Output:

> t.stats
      t-stat
a  1.6124515
b -0.1369306
c  0.8000000

Unless I am mistaken the values you give in your output are not the correct ones.

Or:

t.stats <- data.frame(t.stats)
t.stats$variable <- rownames(t.stats)

> t.stats[,c(2,1)]
  variable     t.stat
a        a  1.6124515
b        b -0.1369306
c        c  0.8000000

If you want a data.frame and a separate column.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!