2012-01-08 52 views
3

我有一個數據框,我正在運行蒙特卡洛模擬,使用for循環來生成模擬分佈。由於我測試的模擬代碼,我只是訪問第一次觀測數據幀:R:我如何從for循環而不是索引輸出因子級別?

Male.MC <-c() 
for (j in 1:100){ 
    for (i in 1:1) { 
     # u2 <- Male.DistF$Male.stddev_u2[i] * rnorm(1, mean = 0, sd = 1) 
     u2 <- Male.DistF$RndmEffct[i] * rnorm(1, mean = 0, sd = 1) 
     mc_bca <- Male.DistF$lmefits[i] + u2 
     temp <- Lambda.Value*mc_bca+1 
     ginv_a <- temp^(1/Lambda.Value) 
     d2ginv_a <- max(0,(1-Lambda.Value)*temp^(1/Lambda.Value-2)) 
     mc_amount <- ginv_a + d2ginv_a * Male.DistF$Male.var[i]^2/2 
     z <- c(RespondentID <- Male.DistF$RespondentID[i], 
        Male.DistF$AgeFactor[i], Male.DistF$SampleWeight[i], 
     Male.DistF$Male.var[i], Male.DistF$lmefits[i], u2, mc_amount) 
     Male.MC <- as.data.frame(rbind(Male.MC,z)) 
    } 
} 
colnames(Male.MC) <- c("RespondentID", "AgeFactor", 
         "SampleWeight", "VarByAge", 
         "lmefits", "u2", "mc_amount") 

代碼精美的作品,除了Male.DistF$RespondentID是一個因素,我沒有得到因素電平輸出,而是得到因子索引,在這種情況下,我得到1,因爲RespondentIDs在Male.DistF數據幀中以升序排列。我與AgeFactor有同樣的問題,我得到的是索引而不是因子水平。

head(Male.MC) 
    RespondentID AgeFactor SampleWeight VarByAge lmefits   u2 mc_amount 
z   1   3 0.4952835 0.4189871 15.22634 0.2334501 11582.681 
2   1   3 0.4952835 0.4189871 15.22634 0.3205741 11984.220 
3   1   3 0.4952835 0.4189871 15.22634 -0.5674165 8420.678 
4   1   3 0.4952835 0.4189871 15.22634 -0.5426489 8505.421 
5   1   3 0.4952835 0.4189871 15.22634 0.4878695 12790.565 
6   1   3 0.4952835 0.4189871 15.22634 0.1556925 11234.583 

如何讓`Male.MC1數據框包含這兩個變量的因子水平?我曾嘗試:

z <- c(RespondentID <- as.character(Male.DistF$RespondentID[i]), 
     Male.DistF$AgeFactor[i], Male.DistF$SampleWeight[i], 
     Male.DistF$Male.var[i], Male.DistF$lmefits[i], u2, mc_amount) 

z <- c((as.character(Male.DistF$RespondentID[i])), 
     Male.DistF$AgeFactor[i], Male.DistF$SampleWeight[i], 
     Male.DistF$Male.var[i], Male.DistF$lmefits[i], u2, mc_amount) 

修復RespondentID輸出,但我做一些錯的語法和它試圖將所有輸出轉換爲因素:

There were 50 or more warnings (use warnings() to see the first 50) 
str(Male.MC) 
'data.frame': 100 obs. of 7 variables: 
$ RespondentID: Factor w/ 1 level "100020": 1 1 1 1 1 1 1 1 1 1 ... 
..- attr(*, "names")= chr "z" "" "" "" ... 
$ AgeFactor : Factor w/ 1 level "3": 1 1 1 1 1 1 1 1 1 1 ... 
..- attr(*, "names")= chr "z" "" "" "" ... 
$ SampleWeight: Factor w/ 1 level "0.495283471": 1 1 1 1 1 1 1 1 1 1 ... 
..- attr(*, "names")= chr "z" "" "" "" ... 
$ VarByAge : Factor w/ 1 level "0.418987052181831": 1 1 1 1 1 1 1 1 1 1 ... 
..- attr(*, "names")= chr "z" "" "" "" ... 
$ lmefits  : Factor w/ 1 level "15.2263403968895": 1 1 1 1 1 1 1 1 1 1 ... 
..- attr(*, "names")= chr "z" "" "" "" ... 
$ u2   : Factor w/ 1 level "-0.100954008424162": 1 NA NA NA NA NA NA NA NA NA ... 
..- attr(*, "names")= chr "z" "" "" "" ... 
$ mc_amount : Factor w/ 1 level "10151.4582133747": 1 NA NA NA NA NA NA NA NA NA ... 
..- attr(*, "names")= chr "z" "" "" "" ... 

對於測試,這裏是輸入數據幀的第一對幾行Male.DistF

 AgeFactor RespondentID SampleWeight IntakeAmt RndmEffct NutrientID Gender Age BodyWeight IntakeDay BoxCoxXY lmefits  lmeres TotWts GrpWts NumSubjects TotSubjects Male.var 
1725  9to13  100020 0.4952835 12145.852 0.30288536  267  1 12  51.6 Day1Intake 15.61196 15.22634 0.27138449 2291.827 763.0604   525  2249 0.4189871 
203  14to18  100419 0.3632839 9591.953 0.02703093  267  1 14  46.3 Day1Intake 15.01444 15.31373 -0.18039624 2291.827 472.3106   561  2249 0.3365423 

Lambda.Value0.1。 上Male.DistF的信息是:

str(Male.DistF) 
'data.frame': 2249 obs. of 18 variables: 
$ AgeFactor : Ord.factor w/ 4 levels "1to3"<"4to8"<..: 3 4 3 4 2 2 3 1 1 3 ... 
$ RespondentID: Factor w/ 2249 levels "100020","100419",..: 1 2 3 4 5 6 7 8 9 10 ... 
$ SampleWeight: num 0.495 0.363 0.495 1.326 2.12 ... 
$ IntakeAmt : num 12146 9592 7839 11113 7150 ... 
$ RndmEffct : num 0.3029 0.027 0.0772 0.4667 -0.1593 ... 
$ NutrientID : int 267 267 267 267 267 267 267 267 267 267 ... 
$ Gender  : int 1 1 1 1 1 1 1 1 1 1 ... 
$ Age   : int 12 14 11 15 6 5 10 2 2 9 ... 
$ BodyWeight : num 51.6 46.3 46.1 63.2 28.4 18 38.2 14.4 14.6 32.1 ... 
$ IntakeDay : Factor w/ 2 levels "Day1Intake","Day2Intake": 1 1 1 1 1 1 1 1 1 1 ... 
$ BoxCoxXY : num 15.6 15 14.5 15.4 14.3 ... 
$ lmefits  : num 15.2 15.3 15 15.8 14.3 ... 
$ lmeres  : num 0.271 -0.18 -0.342 -0.424 -0.053 ... 
$ TotWts  : num 2292 2292 2292 2292 2292 ... 
$ GrpWts  : num 763 472 763 472 779 ... 
$ NumSubjects : int 525 561 525 561 613 613 525 550 550 525 ... 
$ TotSubjects : int 2249 2249 2249 2249 2249 2249 2249 2249 2249 2249 ... 
$ Male.var : num 0.419 0.337 0.419 0.337 0.267 ... 

你可以從我的Male.DistF數據看,在100個重複先觀察,在Male.MC數據幀我想100020作爲RespondentID(而不是1)和9to13作爲AgeFactor(而不是3)。我的輸出指令出錯了,我該如何解決這個問題?尤其是,我不是在追蹤爲什麼我的企圖使用as.character的方式誤入歧途,影響整個產出。另外,我也歡迎有關加快循環的建議。我所做的全部工作就是在我的Male.DistF數據幀中爲每個觀察構建100組值。

回答

4

你可以嘗試更換線

z <- c(... 

它創建新行作爲載體, 即,強制所有元素具有相同的類型, 具有1行data.frame,以保持列的類型。

z <- data.frame(
    RespondentID = Male.DistF$RespondentID[i], 
    AgeFactor = Male.DistF$AgeFactor[i], 
    SampleWeight = Male.DistF$SampleWeight[i], 
    VarByAge  = Male.DistF$Male.var[i], 
    lmefits  = Male.DistF$lmefits[i], 
    u2   = u2, 
    mc_amount = mc_amount 
) 
+0

感謝您的回答,它的工作完美。我在'RespondentID'和'AgeFactor'變量上使用了'as.character'來強制我想要的輸出。我在這個問題上一直在敲我的頭幾個小時。 :) – Michelle 2012-01-08 03:08:08