基于三个月的时间段我有一些数据框的子集,命名为jfm(1月到3月),fma(2月到4月),mam(3月到5月)......直到ond(10月到12月)。我希望使用几个变量作为回归量对所有这些数据进行类似的分析。下面我将展示如何使用其中一种污染物作为回归量对两个子集数据帧中的一个进行分析。我有兴趣对所有污染物(pm10median,pm25median,o3median和so2median)进行分析,每个污染物分别输入模型。如何对所有数据框进行此分析?
library(gamair)
library(mgcv)
data(chicago)
chicago$date<-seq(from=as.Date("1987-01-01"), to=as.Date("2000-12-31"),length=5114)
chicago$month<-as.numeric(format(chicago$date,"%m")) ## create month
jfm <- subset(chicago, month %in% c(1:3) ) ## subset data for January to March
fma <- subset(chicago, month %in% c(2:4) ) ## February to April
mam <- subset(chicago, month %in% c(3:5) ) ## March to may
jfm$trend<-seq(dim(jfm)[1]) ## cretae a trend for specific df based on dimension of the df
fma$trend<-seq(dim(fma)[1]) ## trend for df fma
## Regress each pollutant separately on death for the first subset
model1<-gam(death ~ pm10median + s(trend,k=21)+ s(tmpd,k=6) ,family=quasipoisson,na.action=na.omit,data=jfm)
model2<-gam(death ~ pm10median + s(trend,k=21)+ s(tmpd,k=6) ,family=quasipoisson,na.action=na.omit,data=fma)
答案 0 :(得分:0)
# create a function that defines the exact regression
# you want to run on all three-month data sets
fun <-
function( y , x ){
# store each of the regression outputs into an object
a <- gam(
death ~ pm10median + s(trend,k=21)+ s(tmpd,k=6) ,
family = quasipoisson ,
na.action = na.omit ,
data = x[ x$month %in% y , ]
)
b <- gam(
death ~ pm25median + s(trend,k=21)+ s(tmpd,k=6) ,
family = quasipoisson ,
na.action = na.omit ,
data = x[ x$month %in% y , ]
)
# return each of the regressions as a list
list( a , b )
}
# define which three-month groups you want to run it on
months <- cbind( 1:10 , 2:11 , 3:12 )
# now just run the function for each row in `months`
results <- apply( months , 1 , fun , x = chicago )
# look at the whole thing
results
# extract jfm, for example
jfm <- results[[1]]
# extract fma (and print it to the screen as well)
( fma <- results[[2]] )