# Ilova va t-testdan foydalanganda xato

Kod ichidagi xatolarni va/yoki mantiqdagi kamchilikni topa olmayapman. `R` ichida R X , 0 va 1 ning matritsasi va doimiy qiymatlarning bir `y` vektörü bor va 2 namuna t- X satrlari `y` ning turli guruhlarini ko'rsatadigan kod.

Misol uchun:

``````x = matrix(rbinom(60,1,.5),ncol=10)
y = abs(rnorm(ncol(x)))

apply(x,1,function(x,y=y)t.test(y[x==1],y[x==0]))
``````

Shunday qilib, bu kodni ishlatish uchun `y` ning har bir qatori `y` ning ikkita guruhiga mos keladigan 6 ta t testini olishni kutardim. Biroq, kodni ishlatganimda, bu xato bo'ladi:

`````` Error in t.test(y[x == 1], y[x == 0]) :
promise already under evaluation: recursive default argument reference or earlier problems?
``````

Kimdir men xatolarni tushuntira oladi va kodimni o'zgartirishni istagan narsani olish uchun o'zgartira oladimi?

1

## 6 javoblar

Muammo o'zingizning funktsiyalaringiz argumentlaridagi o'zgarmaydigan nomlarni qayta ishlatishdan kelib chiqadi. Buning uchun:

``````apply(x,1,function(x.f,y.f=y)t.test(y.f[x.f==1],y.f[x.f==0]))
``````
2
qo'shib qo'ydi
```amal qiling (x, 1, funktsiya (x) t.test (y [x == 1], y [x == 0]))```
qo'shib qo'ydi
Qo'shimcha dalillarni bu kabi yo'l bilan o'tkizish juda yoqimli, foydasiz va chalg'ituvchi imho
qo'shib qo'ydi
ikkinchi dalilni nima uchun olib tashlash kerak?
qo'shib qo'ydi
O'ylaymanki, bu nima sodir bo'layotgani yanada aniqroq. Funktsiyani o'z ichiga qamrab oluvchi o'zgaruvchiga havola qilish yaxshi uslublar imho emas.
qo'shib qo'ydi
(x, y) t.test (y [x == 1], y (x == 0]), y = y)
qo'shib qo'ydi
siz `pvalue = sapply (ans, funktsiya (x) x \$ p.value)` degan ma'noni anglatadimi?
qo'shib qo'ydi
Ha! Katta rahmat!
qo'shib qo'ydi
@DavidHeckmann Men p-qiymatlarini olish uchun ```pvalue = lapply (ans,``` [`, c ("p.value"))` dan foydalanmoqdaman, lekin teglar faqat raqamlarning yaxshi vektori kabi ko'rinishi kerakmi?
qo'shib qo'ydi
Yaxshi narsa uning yagona fikridir.
qo'shib qo'ydi

Muammo o'zingizning funktsiyalaringiz argumentlaridagi o'zgarmaydigan nomlarni qayta ishlatishdan kelib chiqadi. Buning uchun:

``````apply(x,1,function(x.f,y.f=y)t.test(y.f[x.f==1],y.f[x.f==0]))
``````
2
qo'shib qo'ydi
ikkinchi dalilni nima uchun olib tashlash kerak?
qo'shib qo'ydi
```amal qiling (x, 1, funktsiya (x) t.test (y [x == 1], y [x == 0]))```
qo'shib qo'ydi
Qo'shimcha dalillarni bu kabi yo'l bilan o'tkizish juda yoqimli, foydasiz va chalg'ituvchi imho
qo'shib qo'ydi
O'ylaymanki, bu nima sodir bo'layotgani yanada aniqroq. Funktsiyani o'z ichiga qamrab oluvchi o'zgaruvchiga havola qilish yaxshi uslublar imho emas.
qo'shib qo'ydi
(x, y) t.test (y [x == 1], y (x == 0]), y = y)
qo'shib qo'ydi
siz `pvalue = sapply (ans, funktsiya (x) x \$ p.value)` degan ma'noni anglatadimi?
qo'shib qo'ydi
Ha! Katta rahmat!
qo'shib qo'ydi
@DavidHeckmann Men p-qiymatlarini olish uchun ```pvalue = lapply (ans,``` [`, c ("p.value"))` dan foydalanmoqdaman, lekin teglar faqat raqamlarning yaxshi vektori kabi ko'rinishi kerakmi?
qo'shib qo'ydi
Yaxshi narsa uning yagona fikridir.
qo'shib qo'ydi

Quyidagi ishlar:

``````> apply(x,1,function(a)t.test(y[a==1],y[a==0]))
[]
``````

X va y va hokazo va umumiy o'zgaruvchilar sifatida ishlatilishi uchun ma'lumotlarni, ma'lumotlar va vektorlarda ma'lumotlarga yaxshi nom berishingiz kerak. Bundan tashqari, funktsiyaga y yuborish kerak emas, chunki barcha testlar uchun bir xil bo'ladi.

Chiqish:

``````        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.43835, df = 5.377, p-value = 0.6782
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.6356057  0.9036413
sample estimates:
mean of x mean of y
0.5807408 0.4467230

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = -0.80208, df = 5.5382, p-value = 0.4555
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.0985419  0.5644195
sample estimates:
mean of x mean of y
0.4337110 0.7007722

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.58194, df = 7.3884, p-value = 0.5779
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.5584942  0.9283034
sample estimates:
mean of x mean of y
0.6329878 0.4480832

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 1.1148, df = 4.8236, p-value = 0.3174
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.4919082  1.2308641
sample estimates:
mean of x mean of y
0.7622223 0.3927443

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.23436, df = 5.5539, p-value = 0.8231
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.7818960  0.9439901
sample estimates:
mean of x mean of y
0.5729543 0.4919073

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = -1.015, df = 7.9168, p-value = 0.3401
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.0152988  0.3954558
sample estimates:
mean of x mean of y
0.3855747 0.6954962
``````

Faqat p qiymatlari uchun:

``````> apply(x,1,function(a)t.test(y[a==1],y[a==0])\$p.value)
 0.6781895 0.4555338 0.5779255 0.3173567 0.8231019 0.3400979
``````
1
qo'shib qo'ydi

Quyidagi ishlar:

``````> apply(x,1,function(a)t.test(y[a==1],y[a==0]))
[]
``````

X va y va hokazo va umumiy o'zgaruvchilar sifatida ishlatilishi uchun ma'lumotlarni, ma'lumotlar va vektorlarda ma'lumotlarga yaxshi nom berishingiz kerak. Bundan tashqari, funktsiyaga y yuborish kerak emas, chunki barcha testlar uchun bir xil bo'ladi.

Chiqish:

``````        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.43835, df = 5.377, p-value = 0.6782
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.6356057  0.9036413
sample estimates:
mean of x mean of y
0.5807408 0.4467230

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = -0.80208, df = 5.5382, p-value = 0.4555
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.0985419  0.5644195
sample estimates:
mean of x mean of y
0.4337110 0.7007722

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.58194, df = 7.3884, p-value = 0.5779
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.5584942  0.9283034
sample estimates:
mean of x mean of y
0.6329878 0.4480832

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 1.1148, df = 4.8236, p-value = 0.3174
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.4919082  1.2308641
sample estimates:
mean of x mean of y
0.7622223 0.3927443

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.23436, df = 5.5539, p-value = 0.8231
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.7818960  0.9439901
sample estimates:
mean of x mean of y
0.5729543 0.4919073

[]

Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = -1.015, df = 7.9168, p-value = 0.3401
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.0152988  0.3954558
sample estimates:
mean of x mean of y
0.3855747 0.6954962
``````

Faqat p qiymatlari uchun:

``````> apply(x,1,function(a)t.test(y[a==1],y[a==0])\$p.value)
 0.6781895 0.4555338 0.5779255 0.3173567 0.8231019 0.3400979
``````
1
qo'shib qo'ydi

Nima haqida

``````apply(x,1,function(x,z)t.test(y[x==1],y[x==0]),y)
``````

If you want to use the second argument within the function, you should also pass it to `apply`

1
qo'shib qo'ydi

Nima haqida

``````apply(x,1,function(x,z)t.test(y[x==1],y[x==0]),y)
``````

If you want to use the second argument within the function, you should also pass it to `apply`

1
qo'shib qo'ydi