我想在 R 中重写 python 代码(实际上是 Jupyter Book)。它是关于计算一些数据的 t 检验函数,以便在使用箱线图之后将其可视化。
我是 Python 和 R 的初学者,但我做了一些尝试。这是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)
蝴蝶刀刀
相关分类