Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从lm函数中获取分组数据的p值_R_Dplyr_Plyr_Lm - Fatal编程技术网

R 从lm函数中获取分组数据的p值

R 从lm函数中获取分组数据的p值,r,dplyr,plyr,lm,R,Dplyr,Plyr,Lm,我正在尝试使用lm()函数和plyr包为数据中的每个段拟合模型,因为我的数据是按键分组的 我已经设法运行了这个模型,得到了系数以及R^2&adj R平方,但是我正在与p值作斗争 library("plyr") #Sample data test_data <- data.frame(key = c("a","a","a","a","a","b","b","b","b","b"), y = c(100,180,120,60,140,200,220,240,260,280), x1 = c(5

我正在尝试使用
lm()
函数和
plyr
包为数据中的每个段拟合模型,因为我的数据是按键分组的

我已经设法运行了这个模型,得到了系数以及R^2&adj R平方,但是我正在与p值作斗争

library("plyr")
#Sample data
test_data <- data.frame(key = c("a","a","a","a","a","b","b","b","b","b"),
y = c(100,180,120,60,140,200,220,240,260,280),
x1 = c(50,60,79,85,90,133,140,120,160,170),
x2 =  c(20,18,47,16,15,25,30,25,20,15))

#model
model_1 <- dlply(test_data, .(key), 
    function(test_data) lm(y ~ x1 +     x2,data = test_data))

#coefficients
ldply(model_1, coef)

#adj r-squared
ldply(model_1, function(x) summary(x)$r.squared)

我曾尝试使用
Do
来拟合模型,然后从
dplyr
包中选择
tidy
,这在一个小数据集上效果很好,因为它实际上返回了我需要的所有数据,但我的实际数据包含1000多个不同的段,RStudio最终崩溃。

我认为不需要
plyr
sapply
就可以了

sapply(model_1, function(x) summary(x)$coefficients[, 4])

                    a         b
(Intercept) 0.3699423 0.3013515
x1          0.7698867 0.7307786
x2          0.9764913 0.3814288
t()
将获得与您的估计相同的配置

顺便说一句,您可能想看看
multidplyr
包,毕竟要处理
tidy
dplyr::do

我正在使用“dplyr”包格式化输出。在“dlply”函数中使用的函数中,应该将summary()用于lm(),因此在调用“coef”时,它还将包括p.values

test_data <- data.frame(key = c("a","a","a","a","a","b","b","b","b","b"),
                        y = c(100,180,120,60,140,200,220,240,260,280),
                        x1 = c(50,60,79,85,90,133,140,120,160,170),
                        x2 =  c(20,18,47,16,15,25,30,25,20,15))

model<-by(test_data,test_data$key,function(x)summary(lm(y~x1+x2,x)))

R2<-t(data.frame(lapply(model,function(x)x$adj.r.squared)));colnames(R2)<-"R2_adj";R2

      R2_adj
a -0.8939647
b  0.4292186

Co<-as.data.frame(t(data.frame(lapply(model,function(x)x$coef))))

colnames(Co)<-c("intercept","x1","x2")

library(dplyr)

Co%>%
        mutate(key=substr(rownames(Co),1,1),
               variable=substr(rownames(Co),3,12))%>%
        select(key,variable,intercept,x1,x2)

  key   variable   intercept         x1          x2
1   a   Estimate 162.1822438 -0.6037364  0.07628315
2   a Std..Error 141.3436897  1.8054132  2.29385395
3   a    t.value   1.1474318 -0.3344035  0.03325545
4   a   Pr...t..   0.3699423  0.7698867  0.97649134
5   b   Estimate 271.0532276  0.3624009 -3.62853907
6   b Std..Error 196.2769562  0.9166979  3.25911570
7   b    t.value   1.3809733  0.3953330 -1.11335080
8   b   Pr...t..   0.3013515  0.7307786  0.38142882

test\u data
nlme::lmList
可能也会让您感兴趣。过来看!除此之外,我非常惊讶地听到
dplyr::do
方法与
broom::tidy
相结合会崩溃。你有没有可能重现这个错误?@Coeffinjunky-抱歉,我说的“崩溃”是指RStudio变得没有反应,没有这样的“错误”。当我在我的数据集上运行dplyr时,它在大约4秒钟内通过1000多个模型,但当我尝试“做”和“整理”时,它运行了近10分钟,RStudio变得没有响应。
test_data <- data.frame(key = c("a","a","a","a","a","b","b","b","b","b"),
                        y = c(100,180,120,60,140,200,220,240,260,280),
                        x1 = c(50,60,79,85,90,133,140,120,160,170),
                        x2 =  c(20,18,47,16,15,25,30,25,20,15))

model<-by(test_data,test_data$key,function(x)summary(lm(y~x1+x2,x)))

R2<-t(data.frame(lapply(model,function(x)x$adj.r.squared)));colnames(R2)<-"R2_adj";R2

      R2_adj
a -0.8939647
b  0.4292186

Co<-as.data.frame(t(data.frame(lapply(model,function(x)x$coef))))

colnames(Co)<-c("intercept","x1","x2")

library(dplyr)

Co%>%
        mutate(key=substr(rownames(Co),1,1),
               variable=substr(rownames(Co),3,12))%>%
        select(key,variable,intercept,x1,x2)

  key   variable   intercept         x1          x2
1   a   Estimate 162.1822438 -0.6037364  0.07628315
2   a Std..Error 141.3436897  1.8054132  2.29385395
3   a    t.value   1.1474318 -0.3344035  0.03325545
4   a   Pr...t..   0.3699423  0.7698867  0.97649134
5   b   Estimate 271.0532276  0.3624009 -3.62853907
6   b Std..Error 196.2769562  0.9166979  3.25911570
7   b    t.value   1.3809733  0.3953330 -1.11335080
8   b   Pr...t..   0.3013515  0.7307786  0.38142882