2013-02-05 22 views
0

我有一些基於三個月的時間段的數據框,名字如jfm(1月至3月),fma(2月至4月),mam(3月至5月)...直到ond(10月至十二月)。我希望使用幾個變量作爲迴歸因子對所有這些數據進行類似的分析。下面我將展示如何使用其中一種污染物作爲迴歸因子對兩個子集數據幀中的一個進行分析。我有興趣對所有分別進入模型的污染物(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

你不必子集,則可以通過JFM,FMA,MAM tapply使用,保理,直到OND ... –

+0

@AdityaSihag例子嗎? :) –

回答

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]]) 
+0

親愛的安東尼, 感謝您的美妙的代碼,它在樣本,以及我自己的數據爲單個迴歸者。你可以添加你的代碼如何循環通過不同的污染物? 順便說一句:我喜歡你的兔子! – Meso

+0

@ user1754610直到您提供可重複的示例。 [閱讀並編輯您的問題](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) –

+0

我指的是樣本芝加哥數據集中的變量(pm10median,pm25median,o3median和so2median)。看我的編輯。 – Meso

相關問題