## Warning: package 'tidyverse' was built under R version 4.3.1
## Warning: package 'ggplot2' was built under R version 4.3.1
## Warning: package 'readr' was built under R version 4.3.1
## Warning: package 'forcats' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
LA <- subset(CASchools, county=="Los Angeles")
head(LA,5)
## district school county grades students teachers
## 19 64709 Lennox Elementary Los Angeles KK-08 6880 303.03
## 33 64816 Mountain View Elementary Los Angeles KK-08 10012 464.90
## 36 64758 Los Nietos Elementary Los Angeles KK-08 2267 103.68
## 46 65078 Valle Lindo Elementary Los Angeles KK-08 1235 53.00
## 51 64592 Hawthorne Elementary Los Angeles KK-08 8935 444.50
## calworks lunch computer expenditure income english read math
## 19 21.2824 94.9712 960 5064.616 7.022000 77.00581 619.1 620.5
## 33 29.7639 91.5934 721 5036.459 8.126616 65.12186 620.8 629.8
## 36 19.1517 84.4338 177 5272.192 11.332500 35.46537 625.4 628.2
## 46 8.2183 62.1302 175 4359.521 7.332000 30.28340 630.2 629.4
## 51 36.9451 79.5120 786 5016.692 14.062000 44.08506 628.4 633.7
Tren1000 <- subset(CASchools, students>1000)
head(Tren1000,5)
## district school county grades students teachers
## 3 61549 Thermalito Union Elementary Butte KK-08 1550 82.90
## 5 61523 Palermo Union Elementary Butte KK-08 1335 71.50
## 10 67306 Del Paso Heights Elementary Sacramento KK-06 2247 108.00
## 19 64709 Lennox Elementary Los Angeles KK-08 6880 303.03
## 20 63560 Lamont Elementary Kern KK-08 2688 135.00
## calworks lunch computer expenditure income english read math
## 3 55.0323 76.3226 169 5501.955 8.978000 30.00000 636.3 650.9
## 5 33.1086 78.4270 171 5235.988 9.080333 13.85768 641.8 639.9
## 10 78.9942 87.3164 0 5036.211 11.613333 40.27592 611.9 613.4
## 19 21.2824 94.9712 960 5064.616 7.022000 77.00581 619.1 620.5
## 20 23.4375 93.2292 139 5433.593 5.699000 49.81399 621.3 619.3
SD <- subset(CASchools, students>1000 & county=="Los Angeles")
head(SD,10)
## district school county grades students teachers
## 19 64709 Lennox Elementary Los Angeles KK-08 6880 303.03
## 33 64816 Mountain View Elementary Los Angeles KK-08 10012 464.90
## 36 64758 Los Nietos Elementary Los Angeles KK-08 2267 103.68
## 46 65078 Valle Lindo Elementary Los Angeles KK-08 1235 53.00
## 51 64592 Hawthorne Elementary Los Angeles KK-08 8935 444.50
## 55 65110 Whittier City Elementary Los Angeles KK-08 7151 318.58
## 56 64477 Eastside Union Elementary Los Angeles KK-08 2404 105.00
## 57 64691 Lawndale Elementary Los Angeles KK-08 5804 283.15
## 67 65037 South Whittier Elementary Los Angeles KK-08 4340 209.84
## 72 64642 Keppel Union Elementary Los Angeles KK-08 2987 141.21
## calworks lunch computer expenditure income english read math
## 19 21.2824 94.9712 960 5064.616 7.022000 77.00581 619.1 620.5
## 33 29.7639 91.5934 721 5036.459 8.126616 65.12186 620.8 629.8
## 36 19.1517 84.4338 177 5272.192 11.332500 35.46537 625.4 628.2
## 46 8.2183 62.1302 175 4359.521 7.332000 30.28340 630.2 629.4
## 51 36.9451 79.5120 786 5016.692 14.062000 44.08506 628.4 633.7
## 55 18.5481 61.7382 560 5245.440 15.404071 31.07258 634.2 629.7
## 56 20.9274 52.9435 202 4838.175 13.762000 18.26123 633.5 630.5
## 57 28.3425 81.1165 480 5367.988 14.184000 34.70021 631.4 633.0
## 67 17.7026 93.9227 284 5181.580 14.601625 31.19816 631.9 636.2
## 72 21.2588 68.3964 273 4826.666 13.730599 14.42919 637.7 631.1
data235 <- S[, c(2,3,5)]
head(data235,5)
## Truong Thanhpho Hocsinh
## 1 Sunol Glen Unified Alameda 195
## 2 Manzanita Elementary Butte 240
## 3 Thermalito Union Elementary Butte 1550
## 4 Golden Feather Union Elementary Butte 243
## 5 Palermo Union Elementary Butte 1335
Hocsinhmoi <- (Hocsinh + Giaovien)
Maytinhmoi <- (CO+2)
Bangdiemx <- cut(M, breaks = c(0, 650, 1000), labels = c("dưới 650 điểm" , "trên 650 điểm"), right = TRUE)
tabdiemx=table(Bangdiemx)
tabdiemx
## Bangdiemx
## dưới 650 điểm trên 650 điểm
## 191 229
b <- rbinom(1000, 20, 191/420)
table(b)
## b
## 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 2 14 36 66 119 166 169 180 117 71 35 20 4 1
hist(b, main="Phân phối nhị phân của biến điểm toán")
BangThuNhap2 <- cut(income, breaks = c(5, 10, 20, 40, 56), labels = c("thap", "trungbinh", "trungbinhcao","cao"), right = TRUE)
tabThuNhap2 = table(BangThuNhap2)
barplot(tabThuNhap2, main= "Biểu đồ biến thu nhập")
### Biểu đồ Strip
stripchart(I, main= "Biểu đồ Strip của biến Thu Nhập", xlab = "Thu Nhập")
stripchart(Hocsinh, main= "Biểu đồ Strip của số học sinh", xlab = "Học sinh")
### Biểu đồ cột cho 2 biến #### + Chú thích: màu đen -> màu xám
trắng: thu nhập thấp -> cao
tabTND = table(BangThuNhap2, Bangdiemx)
barplot(tabTND, beside=TRUE, xlab="Độ tuổi", ylab= "Tần số")
pie(tabThuNhap2, main = "Biểu đồ tròn của biến Thu Nhập")
pie(tabdiemx, main = "Biểu đồ tròn của biến điểm Toán" )
plot(income,Hocsinh)
Phân tích dữ liệu chéo của biến định lượng “students” của dataset: “CASchool” lên biến định lượng “income” thuộc package “AER” # Mô tả dữ liệu - Biến định lượng Income - Biến định lượng Students # Đánh giá dữ liệu
library(AER)
data("CASchools")
### Tạo dữ liệu bản sau “S” từ “CASchools"
S <- CASchools
### Đặt lại tên cho các thuộc tính
names(S) <- c("D","SC","C","G","ST","T","CA","L","CO","E","I","EN","R","M")
mean(S$I)
## [1] 15.31659
median(S$I, na.rm = FALSE)
## [1] 13.7278
var(S$I)
## [1] 52.21348
sum(S$I)
## [1] 6432.967
sd(S$I)
## [1] 7.22589
mean(S$ST)
## [1] 2628.793
median(S$ST, na.rm = FALSE)
## [1] 950.5
sd(S$ST)
## [1] 3913.105
var(S$ST)
## [1] 15312391
sum(S$ST)
## [1] 1104093
I <- S$I
BangI <- cut(I, breaks = c(5, 15, 25, 35, 50 ), labels = c("thấp", "trung bình" , "trung bình cao", "rất cao"), right = TRUE)
tabincome= table(BangI)
tabincome
## BangI
## thấp trung bình trung bình cao rất cao
## 255 132 17 14
ST <- S$ST
BangST <- cut(ST, breaks = c(200,400,600,1000), labels = c("Ít", "Vừa", "Đông"), right = TRUE)
tabde=table(BangST)
tabde
## BangST
## Ít Vừa Đông
## 52 61 46
tabearnde = table(BangI, BangST)
tabearnde
## BangST
## BangI Ít Vừa Đông
## thấp 36 46 26
## trung bình 15 13 15
## trung bình cao 0 0 3
## rất cao 1 2 1
chisq.test(I, ST, correct=FALSE)
## Warning in chisq.test(I, ST, correct = FALSE): Chi-squared approximation may be
## incorrect
##
## Pearson's Chi-squared test
##
## data: I and ST
## X-squared = 131671, df = 131040, p-value = 0.1089
Một khung dữ liệu chứa 420 quan sát trên 14 biến.
district: Mã huyện
school: Tên trường
county: Thành phố
grades: nhân tố biểu thị nhịp độ của huyện.
students: tổng Số học sinh ghi danh
teachers: Số giáo viên
calworks: Phần trăm đủ điều kiện cho hỗ trợ thu nhập
lunch: phần trăm đủ điều kiện cho bữa ăn trưa giảm giá
computer: Số lượng máy tính
expenditure: Chi tiêu cho mỗi sinh viên
income: thu nhập bình quân của huyện (tính theo USD 1.000)
english: tỷ lệ người học tiếng Anh
read: Điểm đọc trung bình
math: Điểm toán trung bình
library(AER)
data("CASchools")
### Tạo dữ liệu bản sau “S” từ “CASchools""
S <- CASchools
### Đặt lại tên cho các thuộc tính
names(S) <- c("D","SC","C","G","ST","T","CA","L","CO","E","I","EN","R","M")
### Xem cấu trúc dữ liệu “CASchools”
str(CASchools)
## 'data.frame': 420 obs. of 14 variables:
## $ district : chr "75119" "61499" "61549" "61457" ...
## $ school : chr "Sunol Glen Unified" "Manzanita Elementary" "Thermalito Union Elementary" "Golden Feather Union Elementary" ...
## $ county : Factor w/ 45 levels "Alameda","Butte",..: 1 2 2 2 2 6 29 11 6 25 ...
## $ grades : Factor w/ 2 levels "KK-06","KK-08": 2 2 2 2 2 2 2 2 2 1 ...
## $ students : num 195 240 1550 243 1335 ...
## $ teachers : num 10.9 11.1 82.9 14 71.5 ...
## $ calworks : num 0.51 15.42 55.03 36.48 33.11 ...
## $ lunch : num 2.04 47.92 76.32 77.05 78.43 ...
## $ computer : num 67 101 169 85 171 25 28 66 35 0 ...
## $ expenditure: num 6385 5099 5502 7102 5236 ...
## $ income : num 22.69 9.82 8.98 8.98 9.08 ...
## $ english : num 0 4.58 30 0 13.86 ...
## $ read : num 692 660 636 652 642 ...
## $ math : num 690 662 651 644 640 ...
head(S)
## D SC C G ST T CA
## 1 75119 Sunol Glen Unified Alameda KK-08 195 10.90 0.5102
## 2 61499 Manzanita Elementary Butte KK-08 240 11.15 15.4167
## 3 61549 Thermalito Union Elementary Butte KK-08 1550 82.90 55.0323
## 4 61457 Golden Feather Union Elementary Butte KK-08 243 14.00 36.4754
## 5 61523 Palermo Union Elementary Butte KK-08 1335 71.50 33.1086
## 6 62042 Burrel Union Elementary Fresno KK-08 137 6.40 12.3188
## L CO E I EN R M
## 1 2.0408 67 6384.911 22.690001 0.000000 691.6 690.0
## 2 47.9167 101 5099.381 9.824000 4.583333 660.5 661.9
## 3 76.3226 169 5501.955 8.978000 30.000002 636.3 650.9
## 4 77.0492 85 7101.831 8.978000 0.000000 651.9 643.5
## 5 78.4270 171 5235.988 9.080333 13.857677 641.8 639.9
## 6 86.9565 25 5580.147 10.415000 12.408759 605.7 605.4
### Lấy biến C của dữ liệu S làm dữ liệu mới
tea <- S$T
### Lấy dữ liệu có điều kiện
tea20 <- tea[tea<20]
tea20den80 <- tea[tea>20&tea<80]
### Xem dòng 3 của dữ liệu tea
tea[3]
## [1] 82.9
### Lấy dữ liệu khi biến E>6000 hoặc biến I<10
teavv <- S[S$E>6000|S$I<10,]
### Thêm 2 biến
S$logEN <- log(S$EN)
S$sumR <- sum(S$R)
### Lập bảng tần số của biến Cou thuộc dữ liệu S
Cou <- S$CO
cut(Cou,5)
## [1] (-3.32,665] (-3.32,665] (-3.32,665]
## [4] (-3.32,665] (-3.32,665] (-3.32,665]
## [7] (-3.32,665] (-3.32,665] (-3.32,665]
## [10] (-3.32,665] (-3.32,665] (-3.32,665]
## [13] (-3.32,665] (-3.32,665] (-3.32,665]
## [16] (-3.32,665] (-3.32,665] (-3.32,665]
## [19] (665,1.33e+03] (-3.32,665] (-3.32,665]
## [22] (-3.32,665] (-3.32,665] (-3.32,665]
## [25] (-3.32,665] (-3.32,665] (665,1.33e+03]
## [28] (-3.32,665] (-3.32,665] (-3.32,665]
## [31] (-3.32,665] (-3.32,665] (665,1.33e+03]
## [34] (-3.32,665] (1.33e+03,1.99e+03] (-3.32,665]
## [37] (-3.32,665] (-3.32,665] (-3.32,665]
## [40] (-3.32,665] (1.33e+03,1.99e+03] (-3.32,665]
## [43] (-3.32,665] (-3.32,665] (-3.32,665]
## [46] (-3.32,665] (1.33e+03,1.99e+03] (-3.32,665]
## [49] (2.66e+03,3.33e+03] (665,1.33e+03] (665,1.33e+03]
## [52] (-3.32,665] (665,1.33e+03] (665,1.33e+03]
## [55] (-3.32,665] (-3.32,665] (-3.32,665]
## [58] (-3.32,665] (-3.32,665] (-3.32,665]
## [61] (-3.32,665] (-3.32,665] (665,1.33e+03]
## [64] (-3.32,665] (-3.32,665] (-3.32,665]
## [67] (-3.32,665] (-3.32,665] (-3.32,665]
## [70] (-3.32,665] (-3.32,665] (-3.32,665]
## [73] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [76] (-3.32,665] (-3.32,665] (-3.32,665]
## [79] (-3.32,665] (-3.32,665] (665,1.33e+03]
## [82] (-3.32,665] (-3.32,665] (-3.32,665]
## [85] (-3.32,665] (-3.32,665] (-3.32,665]
## [88] (665,1.33e+03] (665,1.33e+03] (-3.32,665]
## [91] (-3.32,665] (-3.32,665] (-3.32,665]
## [94] (-3.32,665] (-3.32,665] (-3.32,665]
## [97] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [100] (-3.32,665] (-3.32,665] (-3.32,665]
## [103] (-3.32,665] (-3.32,665] (-3.32,665]
## [106] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [109] (-3.32,665] (-3.32,665] (-3.32,665]
## [112] (665,1.33e+03] (-3.32,665] (-3.32,665]
## [115] (-3.32,665] (-3.32,665] (1.33e+03,1.99e+03]
## [118] (1.33e+03,1.99e+03] (-3.32,665] (-3.32,665]
## [121] (-3.32,665] (-3.32,665] (-3.32,665]
## [124] (-3.32,665] (-3.32,665] (-3.32,665]
## [127] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [130] (-3.32,665] (-3.32,665] (-3.32,665]
## [133] (665,1.33e+03] (-3.32,665] (1.33e+03,1.99e+03]
## [136] (-3.32,665] (-3.32,665] (-3.32,665]
## [139] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [142] (-3.32,665] (1.33e+03,1.99e+03] (-3.32,665]
## [145] (-3.32,665] (-3.32,665] (-3.32,665]
## [148] (-3.32,665] (-3.32,665] (-3.32,665]
## [151] (-3.32,665] (-3.32,665] (-3.32,665]
## [154] (-3.32,665] (-3.32,665] (-3.32,665]
## [157] (665,1.33e+03] (-3.32,665] (665,1.33e+03]
## [160] (2.66e+03,3.33e+03] (-3.32,665] (-3.32,665]
## [163] (-3.32,665] (1.99e+03,2.66e+03] (665,1.33e+03]
## [166] (-3.32,665] (-3.32,665] (-3.32,665]
## [169] (665,1.33e+03] (-3.32,665] (-3.32,665]
## [172] (-3.32,665] (-3.32,665] (-3.32,665]
## [175] (-3.32,665] (-3.32,665] (665,1.33e+03]
## [178] (-3.32,665] (-3.32,665] (-3.32,665]
## [181] (-3.32,665] (-3.32,665] (-3.32,665]
## [184] (-3.32,665] (-3.32,665] (-3.32,665]
## [187] (-3.32,665] (-3.32,665] (-3.32,665]
## [190] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [193] (-3.32,665] (-3.32,665] (-3.32,665]
## [196] (-3.32,665] (-3.32,665] (-3.32,665]
## [199] (-3.32,665] (-3.32,665] (-3.32,665]
## [202] (-3.32,665] (-3.32,665] (-3.32,665]
## [205] (-3.32,665] (-3.32,665] (-3.32,665]
## [208] (-3.32,665] (-3.32,665] (-3.32,665]
## [211] (-3.32,665] (-3.32,665] (-3.32,665]
## [214] (-3.32,665] (-3.32,665] (-3.32,665]
## [217] (-3.32,665] (-3.32,665] (-3.32,665]
## [220] (665,1.33e+03] (-3.32,665] (-3.32,665]
## [223] (-3.32,665] (-3.32,665] (-3.32,665]
## [226] (-3.32,665] (-3.32,665] (-3.32,665]
## [229] (-3.32,665] (-3.32,665] (-3.32,665]
## [232] (-3.32,665] (-3.32,665] (-3.32,665]
## [235] (1.99e+03,2.66e+03] (665,1.33e+03] (-3.32,665]
## [238] (-3.32,665] (-3.32,665] (-3.32,665]
## [241] (-3.32,665] (-3.32,665] (-3.32,665]
## [244] (-3.32,665] (-3.32,665] (-3.32,665]
## [247] (-3.32,665] (-3.32,665] (1.33e+03,1.99e+03]
## [250] (-3.32,665] (-3.32,665] (-3.32,665]
## [253] (-3.32,665] (-3.32,665] (-3.32,665]
## [256] (-3.32,665] (-3.32,665] (-3.32,665]
## [259] (-3.32,665] (-3.32,665] (-3.32,665]
## [262] (-3.32,665] (-3.32,665] (1.33e+03,1.99e+03]
## [265] (-3.32,665] (1.33e+03,1.99e+03] (-3.32,665]
## [268] (-3.32,665] (-3.32,665] (-3.32,665]
## [271] (-3.32,665] (-3.32,665] (-3.32,665]
## [274] (-3.32,665] (-3.32,665] (-3.32,665]
## [277] (-3.32,665] (-3.32,665] (-3.32,665]
## [280] (-3.32,665] (-3.32,665] (-3.32,665]
## [283] (-3.32,665] (-3.32,665] (-3.32,665]
## [286] (-3.32,665] (-3.32,665] (-3.32,665]
## [289] (-3.32,665] (-3.32,665] (-3.32,665]
## [292] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [295] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [298] (665,1.33e+03] (-3.32,665] (-3.32,665]
## [301] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [304] (1.33e+03,1.99e+03] (665,1.33e+03] (-3.32,665]
## [307] (-3.32,665] (-3.32,665] (-3.32,665]
## [310] (-3.32,665] (-3.32,665] (-3.32,665]
## [313] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [316] (-3.32,665] (-3.32,665] (-3.32,665]
## [319] (-3.32,665] (-3.32,665] (-3.32,665]
## [322] (-3.32,665] (-3.32,665] (-3.32,665]
## [325] (-3.32,665] (-3.32,665] (-3.32,665]
## [328] (-3.32,665] (-3.32,665] (-3.32,665]
## [331] (-3.32,665] (-3.32,665] (-3.32,665]
## [334] (-3.32,665] (-3.32,665] (-3.32,665]
## [337] (-3.32,665] (-3.32,665] (-3.32,665]
## [340] (665,1.33e+03] (665,1.33e+03] (-3.32,665]
## [343] (665,1.33e+03] (665,1.33e+03] (-3.32,665]
## [346] (-3.32,665] (665,1.33e+03] (-3.32,665]
## [349] (-3.32,665] (-3.32,665] (-3.32,665]
## [352] (-3.32,665] (-3.32,665] (-3.32,665]
## [355] (-3.32,665] (-3.32,665] (-3.32,665]
## [358] (-3.32,665] (-3.32,665] (-3.32,665]
## [361] (-3.32,665] (-3.32,665] (-3.32,665]
## [364] (-3.32,665] (-3.32,665] (-3.32,665]
## [367] (-3.32,665] (-3.32,665] (-3.32,665]
## [370] (-3.32,665] (-3.32,665] (-3.32,665]
## [373] (-3.32,665] (-3.32,665] (-3.32,665]
## [376] (665,1.33e+03] (-3.32,665] (-3.32,665]
## [379] (-3.32,665] (-3.32,665] (-3.32,665]
## [382] (-3.32,665] (-3.32,665] (-3.32,665]
## [385] (-3.32,665] (-3.32,665] (-3.32,665]
## [388] (-3.32,665] (-3.32,665] (-3.32,665]
## [391] (-3.32,665] (-3.32,665] (-3.32,665]
## [394] (-3.32,665] (-3.32,665] (-3.32,665]
## [397] (-3.32,665] (-3.32,665] (1.99e+03,2.66e+03]
## [400] (-3.32,665] (-3.32,665] (-3.32,665]
## [403] (-3.32,665] (-3.32,665] (-3.32,665]
## [406] (-3.32,665] (-3.32,665] (-3.32,665]
## [409] (-3.32,665] (-3.32,665] (-3.32,665]
## [412] (-3.32,665] (-3.32,665] (-3.32,665]
## [415] (-3.32,665] (-3.32,665] (665,1.33e+03]
## [418] (-3.32,665] (-3.32,665] (-3.32,665]
## 5 Levels: (-3.32,665] (665,1.33e+03] ... (2.66e+03,3.33e+03]
table(cut(Cou,5))
##
## (-3.32,665] (665,1.33e+03] (1.33e+03,1.99e+03] (1.99e+03,2.66e+03]
## 365 39 11 3
## (2.66e+03,3.33e+03]
## 2