R Markdown

# Cài đặt gói nếu chưa có
if (!require("ggplot2")) install.packages("ggplot2")
## Loading required package: ggplot2
# Tạo 100 giá trị ngẫu nhiên từ phân phối nhị thức
set.seed(123)  # Để kết quả có thể tái lập
data <- rbinom(100, size = 10, prob = 0.3)

# Vẽ biểu đồ histogram
library(ggplot2)
ggplot(data = data.frame(x = data), aes(x)) +
  geom_histogram(binwidth = 1, fill = "skyblue", color = "black") +
  labs(title = "Histogram của dữ liệu phân phối nhị thức",
       x = "Giá trị",
       y = "Tần suất") +
  theme_minimal()

# Kiểm định Kolmogorov-Smirnov
ks_test_result <- ks.test(data, "pbinom", size = 10, prob = 0.3)
## Warning in ks.test.default(data, "pbinom", size = 10, prob = 0.3): ties should
## not be present for the one-sample Kolmogorov-Smirnov test
# In kết quả kiểm định
print(ks_test_result)
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  data
## D = 0.26961, p-value = 9.711e-07
## alternative hypothesis: two-sided
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Đọc dữ liệu Iris
from sklearn.datasets import load_iris

iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['species'] = [iris.target_names[i] for i in iris.target]

# Tính toán thống kê
summary_stats = df.groupby("species").agg(['mean', 'median', 'std'])
print(summary_stats)
##            sepal length (cm)                   ... petal width (cm)                 
##                         mean median       std  ...             mean median       std
## species                                        ...                                  
## setosa                 5.006    5.0  0.352490  ...            0.246    0.2  0.105386
## versicolor             5.936    5.9  0.516171  ...            1.326    1.3  0.197753
## virginica              6.588    6.5  0.635880  ...            2.026    2.0  0.274650
## 
## [3 rows x 12 columns]
# Vẽ boxplot
plt.figure(figsize=(12, 6))
for i, col in enumerate(df.columns[:-1]):
    plt.subplot(2, 2, i+1)
    sns.boxplot(x='species', y=col, data=df)
    plt.title(f"Boxplot of {col}")
plt.tight_layout()
plt.show()