dcast函數的問題(reshape2) - 三個變量組合

[英]Issues with dcast function (reshape2) - three variable combination


I am using reshape2 package to shape my data and use it for t-test. For me it is easier to visualize the data in separate columns. I have three treatment combinations where "wat" is nested within "spp" and "ins" is nested within water. My demo table contains 3 response variable namely "tyr", "esc" and "esc_R". I would be interested in seeing how ins influence response -> "tyr" in "spp" -> Bl, with treatment "wat" -> High (just an example).

我正在使用reshape2包來塑造我的數據並將其用於t檢驗。對我來說,在單獨的列中可視化數據更容易。我有三種治療組合,其中“wat”嵌套在“spp”中,“ins”嵌套在水中。我的演示表包含3個響應變量,即“tyr”,“esc”和“esc_R”。我有興趣看看ins如何影響響​​應 - >“tyr”在“spp” - > Bl中,處理“wat” - > High(只是一個例子)。

Here is my data: demo.data

這是我的數據:demo.data

## Use orderBy function to sort data
library(doBy)
demo <- orderBy(~spp+wat+ins, data = demo)
## Create an unique data frame for a specific variable
df.bl.ins.1 <- demo[demo$spp == "Bl", c(1:3, 4)]
df.bl.ins.2 <- df.bl.ins.1[df.bl.ins.1$wat == "High", ]

And then I am having trouble executing dcast function.

然后我在執行dcast功能時遇到問題。

df.bl.ins.tmp <- dcast(df.bl.ins.2, spp + wat ~ ins, value.var = "tyr")

I have found interesting information in the following threads

我在以下主題中找到了有趣的信息

  1. Dason's suggestion - which works really well with ToothGrowth demo dataset. Unfortunately, when the table has multiple treatments (more than 2) the solution did not remain simple. I agree with Maiasaura's suggestions that creating an unique variable is the key to this problem. However, I am having hard time understanding what function(x) does or how to use it in my table.
  2. Dason的建議 - 與ToothGrowth演示數據集配合得非常好。不幸的是,當桌子有多次治療(超過2次)時,解決方案並不簡單。我同意Maiasaura的建議,即創建一個獨特的變量是這個問題的關鍵。但是,我很難理解函數(x)在我的表中做了什么或如何使用它。

Any help in this regard is much appreciated.

在這方面的任何幫助非常感謝。

In addition, if you have alternative suggestions to do t-test without manipulating the original data frame (demo), I will be excited to hear about it.

此外,如果您有其他建議在不操縱原始數據框(演示)的情況下進行t檢驗,我會很高興聽到它。

Thanks in advance.

提前致謝。

Edit Here is what I am expecting, for "tyr". In the following format I desire to compare "No" vs. "Yes" using a t-test.

編輯這是我期待的“tyr”。在以下格式中,我希望使用t檢驗來比較“否”與“是”。

spp wat ins No  Yes
Bl  High    No  0.3036  0.1987
Bl  High    No  0.2577  0.1112
Bl  High    No  NA  0.199
Bl  High    No  0.3299  0.1886
Bl  High    No  0.3301  0.2332

1 个解决方案

#1


1  

Perhaps I don't understand exactly what you want to do, but I think you could run linear regression directly on your data. In this way, you could do t-tests on whether the coefficients of your model were zero or not. I think this might suffice, and serve also to tease apart the effects of each of your independent variables. Here is an example:

也許我不明白你想要做什么,但我認為你可以直接對你的數據進行線性回歸。通過這種方式,您可以對模型的系數是否為零進行t檢驗。我認為這可能就足夠了,也可以用來梳理每個自變量的影響。這是一個例子:

summary(lm(tyr~spp+wat+ins,data=read.table('http://pastebin.com/raw.php?i=sR2MvBBA')))
Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.286386   0.016500  17.356  < 2e-16 ***
sppMan      -0.159514   0.015811 -10.089  1.3e-11 ***
watLow      -0.005501   0.015858  -0.347 0.730861    
insYes      -0.066741   0.015858  -4.209 0.000185 ***

This will get you a t test for just the groups that you showed in your example:

這將使您只測試您在示例中顯示的組:

t.test(tyr~ins,data=df[df$spp=='Bl' & df$wat=='High',])

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/19/72fa17e0aaf7b3da1084234568e068ac.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com