1 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
如前所述,t.test
它是 R 庫中眾多內(nèi)置統(tǒng)計(jì)方法之一stats
。因此,只需構(gòu)建相同的數(shù)據(jù)框,然后運(yùn)行測(cè)試并根據(jù)需要提取測(cè)試統(tǒng)計(jì)數(shù)據(jù)。
數(shù)據(jù)構(gòu)建?(一些需要復(fù)制的爭論pd.DataFrame.from_records()
)
我想在 R 中重寫 python 代碼(實(shí)際上是 Jupyter Book)。它是關(guān)于計(jì)算一些數(shù)據(jù)的 t 檢驗(yàn)函數(shù),以便在使用箱線圖之后將其可視化。
我是 Python 和 R 的初學(xué)者,但我做了一些嘗試。這是Python中的代碼:
import math
import numpy as np
import pandas as pd
from myst_nb import glue
from scipy.stats import ttest_ind
from matplotlib import pyplot as plt
labels = ['non-failing heart (NF)', 'failing heart (F)']
data = [(99, 52), (96, 40), (100, 38), (105, 18),?
? ? ? ? (np.nan, 11), (np.nan, 5), (np.nan, 42),?
? ? ? ? (np.nan, 55), (np.nan, 53), (np.nan, 39),
? ? ? ? (np.nan, 42), (np.nan, 50)]
df = pd.DataFrame.from_records(data, columns=labels)
tt = ttest_ind(df['non-failing heart (NF)'],?
? ? ? ? ? ? ? ?df['failing heart (F)'],?
? ? ? ? ? ? ? ?equal_var=False, nan_policy='omit')
pvalue = tt.pvalue
glue('pvalue', math.ceil(pvalue * 1000.0) / 1000.0)
這是我嘗試過的:
library(math)
labels(data) <- c("non-failing heart (NF)", "failing heart (F)")
library(reticulate)
np <- import("numpy", convert=FALSE)
(x <- np$arange(1, 9)$reshape(2L, 2L))
## [[? 99.? ?52.]
##? ?[? 96.? ?40.]
##? ?[? 100.? ?38.]
##? ?[? 105.? ?18.]
##? ?[ np.nan.? ?11.]
##? ?[ np.nan.? 5.]
##? ?[ np.nan.? 42.]
##? ?[ np.nan.? 55.]
##? ?[ np.nan? 53.]
##? ?[ np.nan? 39.]
##? ?[ np.nan.? 42.]
##? ?[ np.nan? 50.]
##? ?[ 23.? 24.]]
df = pd.DataFrame.from_records(data, columns=labels)
tt = ttest_ind(df['non-failing heart (NF)'],?
? ? ? ? ? ? ? ?df['failing heart (F)'],?
? ? ? ? ? ? ? ?equal_var=False, nan_policy='omit')
pvalue = tt.pvalue
print(pvalue)
添加回答
舉報(bào)