R語言中編寫最小工作示例(MWRE)
原文鏈接:http://tecdat.cn/?p=6716
?
獲得R問題的良好幫助的關(guān)鍵是提供最低限度工作的可重復(fù)示例(MWRE)。使用R制作MWRE非常簡單,它將有助于確保那些幫助您識別錯誤來源的人,并理想地提交給您,以修復(fù)錯誤,而不是向您發(fā)送有用的代碼。要擁有MWRE,您需要以下項目:
產(chǎn)生錯誤的最小數(shù)據(jù)集
生成數(shù)據(jù)所需的最小可運(yùn)行代碼,在提供的數(shù)據(jù)集上運(yùn)行
有關(guān)已使用的軟件包,R版本和系統(tǒng)的必要信息
一個
seed
值,如果隨機(jī)特性是代碼的一部分
讓我們看看R中可用的工具,以幫助我們快速,輕松地創(chuàng)建這些組件。
生成最小數(shù)據(jù)集
這里有三個不同的選項:
使用內(nèi)置R數(shù)據(jù)集
從頭開始創(chuàng)建一個新的vector / data.frame
以可共享的方式輸出您當(dāng)前正在處理的數(shù)據(jù)
讓我們依次看看每一個,看看R幫助我們做的工具。
內(nèi)置數(shù)據(jù)集
R數(shù)據(jù)集中有一些規(guī)范的buit非常適合在幫助請求中使用。
mtcars
?鳶尾花
要查看R中的所有可用數(shù)據(jù)集,只需鍵入:data()
。要加載任何這些數(shù)據(jù)集,只需使用以下內(nèi)容:
data(mtcars)
head(mtcars) ?# to look at the data
mpg cyl disp ?hp drat ? ?wt ?qsec vs am gear carb
Mazda RX4 ? ? ? ? 21.0 ? 6 ?160 110 3.90 2.620 16.46 ?0 ?1 ? ?4 ? ?4
Mazda RX4 Wag ? ? 21.0 ? 6 ?160 110 3.90 2.875 17.02 ?0 ?1 ? ?4 ? ?4
Datsun 710 ? ? ? ?22.8 ? 4 ?108 ?93 3.85 2.320 18.61 ?1 ?1 ? ?4 ? ?1
Hornet 4 Drive ? ?21.4 ? 6 ?258 110 3.08 3.215 19.44 ?1 ?0 ? ?3 ? ?1
Hornet Sportabout 18.7 ? 8 ?360 175 3.15 3.440 17.02 ?0 ?0 ? ?3 ? ?2
Valiant ? ? ? ? ? 18.1 ? 6 ?225 105 2.76 3.460 20.22 ?1 ?0 ? ?3 ? ?1
此選項適用于您知道在R中遇到命令時遇到問題的問題。如果您無法理解為什么您熟悉的命令無法處理數(shù)據(jù),則此選項不是一個很好的選擇。
?
data(stulevel)
names(stulevel)
[1] "X" ? ? ? ? ? "school" ? ? ?"stuid" ? ? ? "grade" ? ? ? "schid"
[6] "dist" ? ? ? ?"white" ? ? ? "black" ? ? ? "hisp" ? ? ? ?"indian"
[11] "asian" ? ? ? "econ" ? ? ? ?"female" ? ? ?"ell" ? ? ? ? "disab"
[16] "sch_fay" ? ? "dist_fay" ? ?"luck" ? ? ? ?"ability" ? ? "measerr"
[21] "teachq" ? ? ?"year" ? ? ? ?"attday" ? ? ?"schoolscore" "district"
[26] "schoolhigh" ?"schoolavg" ? "schoollow" ? "readSS" ? ? ?"mathSS"
[31] "proflvl" ? ? "race"
創(chuàng)建自己的數(shù)據(jù)
?讓我們創(chuàng)建一個學(xué)生考試成績和人口統(tǒng)計學(xué)的模擬數(shù)據(jù)框架。
head(Data)
id gender mathSS readSS race
1 ?1 female ?396.6 ?349.2 ? ?H
2 ?2 ? male ?369.5 ?330.7 ? ?W
3 ?3 female ?423.3 ?354.3 ? ?B
4 ?4 ? male ?348.7 ?333.1 ? ?W
5 ?5 ? male ?299.7 ?353.4 ? ?H
6 ?6 female ?338.0 ?422.1 ? ?I
?我們模擬了學(xué)生數(shù)據(jù)。 讓我們使用快速繪圖來查看變量之間的關(guān)系:
qplot(mathSS, readSS, data = Data, color = race) + theme_bw()

它看起來像比賽是相當(dāng)均勻的分布和存在之間沒有任何關(guān)系mathSS
和readSS
。對于某些應(yīng)用程序,此數(shù)據(jù)已足夠,但對于其他應(yīng)用程序,我們可能希望獲得更實際的數(shù)據(jù)。
table(Data$race)
A ? B ? H ? I ? W
192 195 202 203 208
cor(Data$mathSS, Data$readSS)
[1] -0.01236
輸出您當(dāng)前的數(shù)據(jù)
?這里的最佳實踐是創(chuàng)建您正在處理的數(shù)據(jù)的子集,然后使用該dput
命令輸出它。
dput(head(stulevel, 5))
structure(list(X = c(44L, 53L, 116L, 244L, 274L), school = c(1L,
1L, 1L, 1L, 1L), stuid = c(149995L, 13495L, 106495L, 45205L,
142705L), grade = c(3L, 3L, 3L, 3L, 3L), schid = c(495L, 495L,
495L, 205L, 205L), dist = c(105L, 45L, 45L, 15L, 75L), white = c(0L,
0L, 0L, 0L, 0L), black = c(1L, 1L, 1L, 1L, 1L), hisp = c(0L,
0L, 0L, 0L, 0L), indian = c(0L, 0L, 0L, 0L, 0L), asian = c(0L,
0L, 0L, 0L, 0L), econ = c(0L, 1L, 1L, 1L, 1L), female = c(0L,
0L, 0L, 0L, 0L), ell = c(0L, 0L, 0L, 0L, 0L), disab = c(0L, 0L,
0L, 0L, 0L), sch_fay = c(0L, 0L, 0L, 0L, 0L), dist_fay = c(0L,
0L, 0L, 0L, 0L), luck = c(0L, 1L, 0L, 1L, 0L), ability = c(87.8540493076978,
97.7875614875502, 104.493033823157, 111.671512686787, 81.9253913501755
), measerr = c(11.1332639734731, 6.8223938284885, -7.85615858883968,
-17.5741522573204, 52.9833376218976), teachq = c(39.0902471213577,
0.0984819168655733, 39.5388526976972, 24.1161227728637, 56.6806130368238
), year = c(2000L, 2000L, 2000L, 2000L, 2000L), attday = c(180L,
180L, 160L, 168L, 156L), schoolscore = c(29.2242722609726, 55.9632592971956,
55.9632592971956, 55.9632592971956, 55.9632592971956), district = c(3L,
3L, 3L, 3L, 3L), schoolhigh = c(0L, 0L, 0L, 0L, 0L), schoolavg = c(1L,
1L, 1L, 1L, 1L), schoollow = c(0L, 0L, 0L, 0L, 0L), readSS = c(357.286464546893,
263.904581222636, 369.672179143784, 346.595665384202, 373.125445669888
), mathSS = c(387.280282915207, 302.572371332695, 365.461432571883,
344.496386434725, 441.15810279391), proflvl = structure(c(2L,
3L, 2L, 2L, 2L), .Label = c("advanced", "basic", "below basic",
"proficient"), class = "factor"), race = structure(c(2L, 2L,
2L, 2L, 2L), .Label = c("A", "B", "H", "I", "W"), class = "factor")), .Names = c("X",
"school", "stuid", "grade", "schid", "dist", "white", "black",
"hisp", "indian", "asian", "econ", "female", "ell", "disab",
"sch_fay", "dist_fay", "luck", "ability", "measerr", "teachq",
"year", "attday", "schoolscore", "district", "schoolhigh", "schoolavg",
"schoollow", "readSS", "mathSS", "proflvl", "race"), row.names = c(NA,
5L), class = "data.frame")
生成的代碼可以復(fù)制并粘貼到R ,它將自動按照描述自動構(gòu)建數(shù)據(jù)集。?
?
匿名化您的數(shù)據(jù)
也可能是您想要dput
數(shù)據(jù)的情況,但您希望保持?jǐn)?shù)據(jù)內(nèi)容的匿名性。谷歌搜索提出了一個體面的功能,以實現(xiàn)這一目標(biāo):
anonym <- function(df) {
if (length(df) > 26) {
LETTERS <- c(LETTERS, paste(LETTERS, LETTERS, sep = ""))
})
}
level.id.df <- function(df) {
level.id <- function(i) {
if (class(df[, i]) == "factor" | class(df[, i]) == "character") {
sep = ".")
} else if (is.numeric(df[, i])) {
} else {
column <- df[, i]
}
return(column)
}
DF <- data.frame(sapply(seq_along(df), level.id))
return(DF)
}
df <- level.id.df(df)
return(df)
}
test <- anonym(stulevel)
head(test[, c(2:6, 28:32)])
B ? ? ? ? ? ? ? ? C ? ? ? ? ? ? ? ? D
1 0.00217632592657076 ?1.51160611230132 0.551020408163265
2 0.00217632592657076 0.135998696526593 0.551020408163265
3 0.00217632592657076 ?1.07322572705443 0.551020408163265
4 0.00217632592657076 0.455562880806568 0.551020408163265
5 0.00217632592657076 ?1.43813960635994 0.551020408163265
6 0.00217632592657076 0.151115261535106 0.551020408163265
?
創(chuàng)建示例
一旦我們得到了最小的數(shù)據(jù)集,我們就需要在該數(shù)據(jù)集上重現(xiàn)我們的錯誤。?
讓我們看一個聚合數(shù)據(jù)的錯誤示例。?
Data <- data.frame(id = seq(1, 1000), gender = sample(c("male", "female"), 1000,
replace = TRUE), mathSS = rnorm(1000, mean = 400, sd = 60), readSS = rnorm(1000,
mean = 370, sd = 58.3), race = sample(c("H", "B", "W", "I", "A"), 1000,
replace = TRUE))
myAgg <- Data[, list(meanM = mean(mathSS)), by = race]
Error: unused argument(s) (by = race)
head(myAgg)
Error: object 'myAgg' not found
為什么我會收到錯誤? 如果您將上述代碼發(fā)送給某人,他們可以快速評估錯誤,如果他們知道您正在嘗試使用data.table包,請查看錯誤。
library(data.table)
Data <- data.frame(id = seq(1, 1000), gender = sample(c("male", "female"), 1000,
replace = TRUE), mathSS = rnorm(1000, mean = 400, sd = 60), readSS = rnorm(1000,
mean = 370, sd = 58.3), race = sample(c("H", "B", "W", "I", "A"), 1000,
replace = TRUE))
Data <- data.table(Data)
myAgg <- Data[, list(meanM = mean(mathSS)), by = race]
head(myAgg)
race meanM
1: ? ?H 398.6
2: ? ?B 405.1
3: ? ?A 397.8
4: ? ?W 395.7
5: ? ?I 399.1
會話信息
但是,他們可能不知道這一點(diǎn),所以我們需要提供最后一條信息。 要診斷錯誤,必須知道您正在運(yùn)行的系統(tǒng),工作區(qū)中加載了哪些軟件包,以及您使用的R版本和給定軟件包。
?只需添加sessionInfo()
功能的輸出?。這很容易復(fù)制和粘貼或包含在knitr
文檔中。
sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base
other attached packages:
[1] data.table_1.8.8 eeptools_0.2 ? ? ggplot2_0.9.3.1 ?knitr_1.2
loaded via a namespace (and not attached):
[1] colorspace_1.2-2 ? dichromat_2.0-0 ? ?digest_0.6.3
[4] evaluate_0.4.3 ? ? formatR_0.7 ? ? ? ?grid_2.15.2
[7] gtable_0.1.2 ? ? ? labeling_0.1 ? ? ? MASS_7.3-23
[10] munsell_0.4 ? ? ? ?plyr_1.8 ? ? ? ? ? proto_0.3-10
[13] RColorBrewer_1.0-5 reshape2_1.2.2 ? ? scales_0.2.3
[16] stringr_0.6.2 ? ? ?tools_2.15.2
?
?