Skip to content
Prev 317747 / 398506 Next

Select components of a list

Hi Gustav,
Just change `summary(x)$coef` to `summary(x)$p.table`
I am pasting the code from the attachment.

library(gamair)? 
library(mgcv) 
data(chicago)? 
library(splines) 
?
chicago$date<-seq(from=as.Date("1987-01-01"), to=as.Date("2000-12-31"),length=5114) 
?
chicago$trend<-seq(dim(chicago)[1])? 
names(chicago) [2] <-"pm10" 
names(chicago) [3] <-"pm25" 
names(chicago) [4] <-"ozone" 
names(chicago) [5] <-"so2" 
names(chicago) [7]?? <-"temp" 
?
chicago$trend<-seq(dim(chicago)[1]) 
chicago$year<-as.numeric(format(chicago$date,"%Y"))? 
chicago1<-subset(chicago, as.Date(date) < '1999-01-01')? 
year<- matrix(1987:1998, ncol=3, byrow=TRUE) 

fun <-? 
? function( y , x ){ 
??? a <- gam( 
????? death ~ pm10 + s(trend,k=35) , poisson , na.action = na.omit , data = x[ x$year %in% y , ] 
??? ) 
??? b<- gam( 
????? death ~ ozone + s(trend,k=35), poisson , na.action = na.omit , data = x[ x$year %in% y , ] 
??? ) 
??? c<- gam ( 
????? death ~ so2 + ns(trend,k=35) , poisson , na.action = na.omit , data = x[ x$year %in% y , ] 
??? ) 
??? list( a , b ,c) 
? } 

models<- apply(year, 1 , fun , x = chicago )

#solution
apply(1:length(models),function(i) lapply(models[[i]],function(x) summary(x)$p.table[2,]))[[1]] #1st list component
#[[1]]
#??? Estimate?? Std. Error????? z value???? Pr(>|z|) 
#6.054413e-04 1.474943e-04 4.104845e+00 4.045864e-05 
#
#[[2]]
#??? Estimate?? Std. Error????? z value???? Pr(>|z|) 
#0.0009999765 0.0003777224 2.6473846529 0.0081117027 

#[[3]]
#??? Estimate?? Std. Error????? z value???? Pr(>|z|) 
#5.643234e-03 9.023766e-04 6.253746e+00 4.007234e-10 

res<-lapply(1:length(models),function(i) do.call(rbind,lapply(models[[i]],function(x) summary(x)$p.table[row.names(summary(x)$p.table)%in%c("pm10","ozone","so2"),c(1:2,4)]))) 
?names(res)<-1:length(res) 
res1<- lapply(res,function(i) {row.names(i)<-c("pm10","ozone","so2");data.frame(i)})

library(abind)
res2<-abind(res1,along=1,hier.names=T)? #gives a matrix
colnames(res2)[2:3]<- c("Std.Error","Pr(>|z|)")

res3<- do.call(rbind,lapply(res,function(i) {row.names(i)<-c("pm10","ozone","so2");data.frame(i)}))
colnames(res3)[2:3]<- c("Std.Error","Pr(>|z|)")

?str(res2)
?#num [1:12, 1:3] 0.000605 0.001 0.005643 0.00059 0.000839 ...
?#- attr(*, "dimnames")=List of 2
?# ..$ : chr [1:12] "1.pm10" "1.ozone" "1.so2" "2.pm10" ...
?# ..$ : chr [1:3] "Estimate" "Std.Error" "Pr(>|z|)"

str(res3)
#'data.frame':??? 12 obs. of? 3 variables:
# $ Estimate : num? 0.000605 0.001 0.005643 0.00059 0.000839 ...
# $ Std.Error: num? 0.000147 0.000378 0.000902 0.000172 0.000427 ...
?#$ Pr(>|z|) : num? 4.05e-05 8.11e-03 4.01e-10 6.23e-04 4.96e-02 ...

res2
#??????????? Estimate??? Std.Error???? Pr(>|z|)
#1.pm10? 0.0006054413 0.0001474943 4.045864e-05
#1.ozone 0.0009999765 0.0003777224 8.111703e-03
#1.so2?? 0.0056432338 0.0009023766 4.007234e-10
#2.pm10? 0.0005899052 0.0001724085 6.226404e-04
#2.ozone 0.0008389801 0.0004272480 4.956676e-02
#2.so2?? 0.0032899751 0.0009475318 5.163027e-04
#3.pm10? 0.0005398889 0.0001551911 5.035438e-04
#3.ozone 0.0023890220 0.0004082119 4.845107e-09
#3.so2?? 0.0049121476 0.0008818088 2.539574e-08
#4.pm10? 0.0009341888 0.0001760271 1.113999e-07
#4.ozone 0.0005461742 0.0004253987 1.991731e-01
#4.so2?? 0.0055712219 0.0011123419 5.484117e-07

Hope it helps.
A.K.
Message-ID: <1361114604.4678.YahooMailNeo@web142601.mail.bf1.yahoo.com>
In-Reply-To: <CAFqDh9+aMFvdgk_Nomr4TYDFbF64U_F-cqjLSjVKvMRwe9Fttg@mail.gmail.com>