获取摘要对象行
get row of summary object
我正在尝试获取 估计值和标准值的数值。摘要对象出错,但我在应用中访问了错误的行(我认为)。我该如何解决这个问题?
require(MASS)
colnames(Cars93)
ll<-lm(Fuel.tank.capacity~Length*Passengers,data=Cars93)
s<-summary(ll)$coeff
apply(s,1,function(x){
paste(x[1]+3,x[2]+3) #trying to return intercept and length for each row
})
目标:
-8.18 17.93
0.11 0.09
.....
以下代码 return 是一个(命名的)字符串列表,其中包含估计值、space 和截距:
> apply(s[,c(1,2)],1,paste,collapse = " ")
(Intercept) Length
"-8.18006974657324 17.9310806930227" "0.119717136183204 0.0994386410426733"
Passengers Length:Passengers
"-0.00836397172098245 3.55247972849905" "0.00314614268107023 0.019412665227971"
这不完全是您要求的,因为它仍然是一个命名矢量,但您可以根据需要使用另一个 paste(collapse="\n")
将其余部分粘贴在一起。请注意,字符串中的 \n
是回车符 return,但在解释器中显示时,它只是打印为 \n。
对象's'是一个矩阵
is.matrix(s)
[1] TRUE
因此,您通过以下方式从此对象中提取拦截和标准错误:
s[,1:2]
Estimate Std. Error
(Intercept) -8.180069747 17.93108069
Length 0.119717136 0.09943864
Passengers -0.008363972 3.55247973
Length:Passengers 0.003146143 0.01941267
以下代码应该可以满足您的需求:
t(apply(s,1,function(x){
c(x[1],x[2])
}))
Estimate Std. Error
(Intercept) -8.180069747 17.93108069
Length 0.119717136 0.09943864
Passengers -0.008363972 3.55247973
Length:Passengers 0.003146143 0.01941267
我正在尝试获取 估计值和标准值的数值。摘要对象出错,但我在应用中访问了错误的行(我认为)。我该如何解决这个问题?
require(MASS)
colnames(Cars93)
ll<-lm(Fuel.tank.capacity~Length*Passengers,data=Cars93)
s<-summary(ll)$coeff
apply(s,1,function(x){
paste(x[1]+3,x[2]+3) #trying to return intercept and length for each row
})
目标:
-8.18 17.93
0.11 0.09
.....
以下代码 return 是一个(命名的)字符串列表,其中包含估计值、space 和截距:
> apply(s[,c(1,2)],1,paste,collapse = " ")
(Intercept) Length
"-8.18006974657324 17.9310806930227" "0.119717136183204 0.0994386410426733"
Passengers Length:Passengers
"-0.00836397172098245 3.55247972849905" "0.00314614268107023 0.019412665227971"
这不完全是您要求的,因为它仍然是一个命名矢量,但您可以根据需要使用另一个 paste(collapse="\n")
将其余部分粘贴在一起。请注意,字符串中的 \n
是回车符 return,但在解释器中显示时,它只是打印为 \n。
对象's'是一个矩阵
is.matrix(s)
[1] TRUE
因此,您通过以下方式从此对象中提取拦截和标准错误:
s[,1:2]
Estimate Std. Error
(Intercept) -8.180069747 17.93108069
Length 0.119717136 0.09943864
Passengers -0.008363972 3.55247973
Length:Passengers 0.003146143 0.01941267
以下代码应该可以满足您的需求:
t(apply(s,1,function(x){
c(x[1],x[2])
}))
Estimate Std. Error
(Intercept) -8.180069747 17.93108069
Length 0.119717136 0.09943864
Passengers -0.008363972 3.55247973
Length:Passengers 0.003146143 0.01941267