Building a ROC producing function in R - r

I want to make a function that accepts an xgboost model and the data (test set that I already got, called mydata[[2]]) and returns a ROC curve.
My first try was like this:
evaluate_model_plot = function(model,data) {
plt = roc(data[,1],predict(model, data[,-1]))
plot(plt, main = 'Test Set')
}
I get this error:
> evaluate_model_plot(myxgb, mydata[[2]])
Error in xgb.DMatrix(newdata, missing = missing) :
xgb.DMatrix does not support construction from list
I tried again:
evaluate_model_plot = function(model,data) {
dtest = xgb.DMatrix(data = data.matrix(data[,-1]),label=as.numeric(as.character(data[,1])))
plt = roc(dtest,predict(model, dtest))
plot(plt, main = 'Test Set')
}
Now the error is:
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
I'm not sure how to fix this. The first error is not very clear to me since the data I'm providing is a data frame, it's saying that it doesn't accept a list, although the input is not a list..
Here is a sample from my test set:
> dput(data[[2]][1:15,1:5])
structure(list(CR = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("0", "1"), class = "factor"),
CD4.T.cells = c(0.0410284948786532, -0.0366277340916379,
-0.0349008907108641, -0.062499826731091, -0.206241592996545,
-0.0874700869552125, -0.182120904486964, -0.171941115538424,
-0.101887370292612, -0.0471260521659922, 0.00336508376392719,
-0.0558977705951249, -0.0346269111530533, 0.0405376992753275,
-0.116824556352525), CD8.T.cells = c(-0.0372301980787381,
-0.0550538743643369, -0.157003425700701, -0.280984614146961,
-0.245432535063266, -0.147192190940429, -0.244633652045209,
-0.213892169574032, -0.301036923607255, -0.427604924497491,
-0.371628492024595, -0.0166106304573527, -0.200413064153971,
-0.276715360733834, -0.13198989690999), T.helpers = c(0.186447606591857,
0.0686746776877563, -0.0662055287009653, -0.0969110962088053,
-0.176807066557773, -0.079679981476819, -0.0642576894506894,
-0.0543810984331623, -0.0741755574175595, -0.0131228323048211,
-0.214234307929943, -0.0137492004706794, -0.000982317887326278,
-0.126128005295858, -0.177662982037535), NK.cells = c(-0.0172673823614314,
-0.0518877213975413, -0.129207379606117, -0.174457678991973,
-0.103184559349521, -0.0415572732935339, -0.100644327844509,
-0.124636632740142, -0.131184498665394, -0.181979331198521,
0.0352080568467581, -0.00354359170704435, -0.0820137078499403,
-0.167969284367181, 0.132078267822988)), row.names = c("Pt101",
"Pt18", "Pt2", "Pt3", "Pt48", "Pt59", "Pt67", "Pt82", "Pt98",
"EA595647", "EA632133", "EA632149", "EA632171", "EA639018", "G109543_RCCBMS-00114-T_v1_RNA_OnPrem"
), class = "data.frame")

Related

Any efficient way to label the data points along top 3 PCA in 3D plot in R?

I am interested in labeling data points which are spanned through 3 top PCA components. Since I have done with PCA on my data, I am curious how to specify the label of data points that belongs to a different category and visualize them in the 3D plot. To manipulate data, I used dplyr, 3dscatterplot for rendering 3D plot which supposed to display data points with specific colors. I still didn't get what I expected. I looked into SO and people indeed visualize 3D scatter plot. can anyone point me out how to make this happen? Is there any efficient way to label the data points along with top 3 PCA components in 3D scatter plot? any thoughts?
reproducible data
Here is my reproducibel data:
> dput(head(my_df,15))
structure(list(SampleID = structure(c(33L, 42L, 52L, 61L, 70L,
79L, 71L, 80L, 89L, 98L, 35L, 81L, 90L, 99L, 36L), .Label = c("Sample_10",
"Sample_11", "Sample_12", "Sample_13", "Sample_14", "Sample_15",
"Sample_18", "Sample_19", "Sample_20", "Sample_21", "Sample_22",
"Sample_23", "Sample_24", "Sample_25", "Sample_26", "Sample_27",
"Sample_28", "Sample_29", "Sample_3", "Sample_30", "Sample_31",
"Sample_32", "Sample_34", "Sample_36", "Sample_37", "Sample_38",
"Sample_4", "Sample_5", "Sample_6", "Sample_7", "Sample_8", "Sample_9",
"Tarca_001_P1A01", "Tarca_003_P1A03", "Tarca_004_P1A04", "Tarca_005_P1A05",
"Tarca_006_P1A06", "Tarca_007_P1A07", "Tarca_008_P1A08", "Tarca_011_P1A11",
"Tarca_012_P1A12", "Tarca_013_P1B01", "Tarca_015_P1B03", "Tarca_016_P1B04",
"Tarca_017_P1B05", "Tarca_018_P1B06", "Tarca_019_P1B07", "Tarca_020_P1B08",
"Tarca_022_P1B10", "Tarca_023_P1B11", "Tarca_024_P1B12", "Tarca_025_P1C01",
"Tarca_027_P1C03", "Tarca_028_P1C04", "Tarca_029_P1C05", "Tarca_030_P1C06",
"Tarca_031_P1C07", "Tarca_034_P1C10", "Tarca_035_P1C11", "Tarca_036_P1C12",
"Tarca_037_P1D01", "Tarca_039_P1D03", "Tarca_040_P1D04", "Tarca_041_P1D05",
"Tarca_042_P1D06", "Tarca_043_P1D07", "Tarca_046_P1D10", "Tarca_047_P1D11",
"Tarca_048_P1D12", "Tarca_049_P1E01", "Tarca_051_P1E03", "Tarca_052_P1E04",
"Tarca_053_P1E05", "Tarca_054_P1E06", "Tarca_055_P1E07", "Tarca_058_P1E10",
"Tarca_059_P1E11", "Tarca_060_P1E12", "Tarca_061_P1F01", "Tarca_063_P1F03",
"Tarca_064_P1F04", "Tarca_065_P1F05", "Tarca_066_P1F06", "Tarca_067_P1F07",
"Tarca_070_P1F10", "Tarca_071_P1F11", "Tarca_072_P1F12", "Tarca_074_P1G02",
"Tarca_075_P1G03", "Tarca_076_P1G04", "Tarca_077_P1G05", "Tarca_078_P1G06",
"Tarca_079_P1G07", "Tarca_082_P1G10", "Tarca_083_P1G11", "Tarca_084_P1G12",
"Tarca_086_P1H02", "Tarca_087_P1H03", "Tarca_088_P1H04", "Tarca_089_P1H05",
"Tarca_090_P1H06", "Tarca_091_P1H07", "Tarca_094_P1H10", "Tarca_095_P1H11",
"Tarca_096_P1H12", "Tarca_100_P2D01", "Tarca_101_P2E01", "Tarca_102_P2F01",
"Tarca_103_P2G01", "Tarca_106_P2B02", "Tarca_107_P2C02", "Tarca_108_P2D02",
"Tarca_111_P2G02", "Tarca_114_P2B03", "Tarca_115_P2C03", "Tarca_116_P2D03",
"Tarca_117_P2E03", "Tarca_120_P2H03_2", "Tarca_125_P2E04", "Tarca_126_P2F04",
"Tarca_127_P2G04", "Tarca_128_P2H04", "Tarca_132_P2D05", "Tarca_133_P2E05",
"Tarca_135_P2G05", "Tarca_136_P2H05", "Tarca_137_P2A06", "Tarca_138_P2B06",
"Tarca_139_P2C06", "Tarca_140_P2D06", "Tarca_141_P2E06", "Tarca_144_P2H06",
"Tarca_145_P2A07", "Tarca_146_P2B07", "Tarca_147_P2C07", "Tarca_148_P2D07",
"Tarca_149_P2E07", "Tarca_150_P2F07", "Tarca_151_P2G07", "Tarca_152_P2H07",
"Tarca_153_P2A08", "Tarca_154_P2B08", "Tarca_155_P2C08", "Tarca_156_P2D08",
"Tarca_157_P2E08", "Tarca_158_P2F08", "Tarca_159_P2G08", "Tarca_160_P2H08",
"Tarca_162_P2B09", "Tarca_163_P2C09", "Tarca_165_P2E09", "Tarca_166_P2F09",
"Tarca_167_P2G09", "Tarca_168_P2H09", "Tarca_169_P2A10", "Tarca_170_P2B10",
"Tarca_173_P2E10", "Tarca_174_P2F10", "Tarca_175_P2G10", "Tarca_176_P2H10",
"Tarca_177_P2A11", "Tarca_178_P2B11", "Tarca_179_P2C11", "Tarca_180_P2D11",
"Tarca_183_P2G11", "Tarca_184_P2H11", "Tarca_185_P2A12", "Tarca_186_P2B12",
"Tarca_188_P2D12", "Tarca_189_P2E12", "Tarca_190_P2F12", "Tarca_192_P2H12",
"Tarca_195_P3C01", "Tarca_196_P3D01", "Tarca_197_P3E01", "Tarca_199_P3G01",
"Tarca_200_P3H01", "Tarca_201_P3A02", "Tarca_202_P3B02", "Tarca_203_P3C02",
"Tarca_204_P3D02", "Tarca_205_P3E02", "Tarca_206_P3F02", "Tarca_207_P3G02",
"Tarca_208_P3H02", "Tarca_209_P3A03", "Tarca_210_P3B03", "Tarca_211_P3C03",
"Tarca_212_P3D03", "Tarca_213_P3E03", "Tarca_214_P3F03", "Tarca_216_P3H03",
"Tarca_217_P3A04", "Tarca_218_P3B04", "Tarca_219_P3C04", "Tarca_223_P3G04",
"Tarca_224_P3H04", "Tarca_225_P3A05", "Tarca_226_P3B05", "Tarca_227_P3C05",
"Tarca_228_P3D05", "Tarca_229_P3E05", "Tarca_230_P3F05", "Tarca_231_P3G05",
"Tarca_232_P3H05", "Tarca_233_P3A06", "Tarca_234_P3B06", "Tarca_235_P3C06",
"Tarca_236_P3D06", "Tarca_237_P3E06", "Tarca_238_P3F06", "Tarca_239_P3G06",
"Tarca_240_P3H06", "Tarca_241_P3A07", "Tarca_242_P3B07", "Tarca_243_P3C07",
"Tarca_244_P3D07", "Tarca_245_P3E07", "Tarca_246_P3F07", "Tarca_249_P3A08",
"Tarca_250_P3B08", "Tarca_251_P3C08", "Tarca_252_P3D08", "Tarca_255_P3G08",
"Tarca_256_P3H08", "Tarca_258_P3B09", "Tarca_259_P3C09", "Tarca_261_P3E09",
"Tarca_262_P3F09", "Tarca_264_P3H09", "Tarca_266_P3B10", "Tarca_267_P3C10",
"Tarca_268_P3D10", "Tarca_269_P3E10", "Tarca_270_P3F10", "Tarca_271_P3G10",
"Tarca_272_P3H10", "Tarca_274_P3B11", "Tarca_275_P3C11", "Tarca_279_P3G11",
"Tarca_280_P3H11", "Tarca_282_P3B12", "Tarca_283_P3C12", "Tarca_289_P4A01",
"Tarca_290_P4B01", "Tarca_291_P4C01", "Tarca_292_P4D01", "Tarca_293_P4E01",
"Tarca_294_P4F01", "Tarca_295_P4G01", "Tarca_296_P4H01", "Tarca_297_P4A02",
"Tarca_298_P4B02", "Tarca_302_P4F02", "Tarca_305_P4A03", "Tarca_306_P4B03",
"Tarca_307_P4C03", "Tarca_310_P4F03", "Tarca_311_P4G03", "Tarca_312_P4H03",
"Tarca_313_P4A04", "Tarca_314_P4B04", "Tarca_315_P4C04", "Tarca_316_P4D04",
"Tarca_317_P4E04", "Tarca_319_P4G04", "Tarca_320_P4H04", "Tarca_321_P4A05",
"Tarca_324_P4D05", "Tarca_325_P4E05", "Tarca_326_P4F05", "Tarca_327_P4G05",
"Tarca_328_P4H05", "Tarca_329_P4A06", "Tarca_330_P4B06", "Tarca_331_P4C06",
"Tarca_332_P4D06", "Tarca_333_P4E06", "Tarca_334_P4F06", "Tarca_335_P4G06",
"Tarca_336_P4H06", "Tarca_337_P4A07", "Tarca_338_P4B07", "Tarca_339_P4C07",
"Tarca_340_P4D07", "Tarca_341_P4E07", "Tarca_342_P4F07", "Tarca_343_P4G07",
"Tarca_344_P4H07", "Tarca_345_P4A08", "Tarca_346_P4B08", "Tarca_347_P4C08",
"Tarca_348_P4D08", "Tarca_349_P4E08", "Tarca_350_P4F08", "Tarca_351_P4G08",
"Tarca_352_P4H08", "Tarca_354_P4B09", "Tarca_356_P4D09", "Tarca_357_P4E09",
"Tarca_359_P4G09", "Tarca_360_P4H09", "Tarca_361_P4A10", "Tarca_362_P4B10",
"Tarca_363_P4C10", "Tarca_364_P4D10", "Tarca_366_P4F10", "Tarca_367_P4G10",
"Tarca_369_P4A11", "Tarca_370_P4B11", "Tarca_371_P4C11", "Tarca_372_P4D11",
"Tarca_373_P4E11", "Tarca_374_P4F11", "Tarca_375_P4G11", "Tarca_377_P4A12",
"Tarca_378_P4B12", "Tarca_379_P4C12", "Tarca_380_P4D12", "Tarca_381_P4E12",
"Tarca_382_P4F12", "Tarca_383_P4G12", "Tarca_384_P4H12", "Tarca_385_P5A01",
"Tarca_387_P5C01", "Tarca_389_P5E01", "Tarca_390_P5F01", "Tarca_391_P5G01",
"Tarca_395_P5C02", "Tarca_396_P5D02", "Tarca_397_P5E02", "Tarca_398_P5F02",
"Tarca_399_P5G02", "Tarca_400_P5H02", "Tarca_401_P5A03", "Tarca_402_P5B03",
"Tarca_404_P5D03", "Tarca_407_P5G03", "Tarca_408_P5H03", "Tarca_409_P5A04",
"Tarca_410_P5B04", "Tarca_411_P5C04", "Tarca_412_P5D04", "Tarca_413_P5E04",
"Tarca_414_P5F04", "Tarca_415_P5G04", "Tarca_416_P5H04", "Tarca_418_P5B05",
"Tarca_419_P5C05", "Tarca_422_P5F05", "Tarca_423_P5G05", "Tarca_424_P5H05",
"Tarca_425_P5A06", "Tarca_426_P5B06", "Tarca_428_P5D06", "Tarca_430_P5F06",
"Tarca_432_P5H06", "Tarca_435_P5C07", "Tarca_436_P5D07", "Tarca_437_P5E07",
"Tarca_438_P5F07", "Tarca_439_P5G07", "Tarca_440_P5H07", "Tarca_441_P5A08",
"Tarca_445_P5E08", "Tarca_446_P5F08", "Tarca_447_P5G08", "Tarca_448_P5H08",
"Tarca_449_P5A09", "Tarca_450_P5B09", "Tarca_451_P5C09", "Tarca_452_P5D09",
"Tarca_453_P5E09", "Tarca_454_P5F09", "Tarca_456_P5H09", "Tarca_457_P5A10",
"Tarca_458_P5B10", "Tarca_459_P5C10", "Tarca_460_P5D10", "Tarca_461_P5E10",
"Tarca_462_P5F10", "Tarca_463_P5G10", "Tarca_464_P5H10", "Tarca_465_P5A11",
"Tarca_467_P5C11", "Tarca_468_P5D11", "Tarca_469_P5E11", "Tarca_470_P5F11",
"Tarca_471_P5G11", "Tarca_472_P5H11", "Tarca_474_P5B12", "Tarca_475_P5C12",
"Tarca_476_P5D12", "Tarca_477_P5E12", "Tarca_478_P5F12", "Tarca_479_P5G12",
"Tarca_480_P5H12", "Tarca_481_P6A01", "Tarca_482_P6B01", "Tarca_486_P6F01",
"Tarca_487_P6G01", "Tarca_488_P6H01", "Tarca_489_P6A02", "Tarca_490_P6B02",
"Tarca_491_P6C02", "Tarca_492_P6D02", "Tarca_493_P6E02", "Tarca_494_P6F02",
"Tarca_496_P6H02", "Tarca_497_P6A03", "Tarca_498_P6B03", "Tarca_499_P6C03",
"Tarca_500_P6D03", "Tarca_501_P6E03", "Tarca_502_P6F03", "Tarca_503_P6G03",
"Tarca_504_P6H03", "Tarca_505_P6A04", "Tarca_506_P6B04", "Tarca_509_P6E04",
"Tarca_510_P6F04", "Tarca_511_P6G04", "Tarca_514_P6B05", "Tarca_515_P6C05",
"Tarca_517_P6E05", "Tarca_518_P6F05", "Tarca_519_P6G05", "Tarca_523_P6C06",
"Tarca_524_P6D06", "Tarca_526_P6F06", "Tarca_527_P6G06", "Tarca_528_P6H06",
"Tarca_529_P6A07", "Tarca_530_P6B07", "Tarca_532_P6D07_2", "Tarca_535_P6G07",
"Tarca_536_P6H07", "Tarca_537_P6A08", "Tarca_538_P6B08", "Tarca_539_P6C08",
"Tarca_540_P6D08", "Tarca_541_P6E08", "Tarca_542_P6F08", "Tarca_543_P6G08",
"Tarca_544_P6H08", "Tarca_545_P6A09", "Tarca_546_P6B09", "Tarca_547_P6C09",
"Tarca_548_P6D09", "Tarca_549_P6E09", "Tarca_550_P6F09", "Tarca_551_P6G09",
"Tarca_552_P6H09", "Tarca_553_P6A10", "Tarca_554_P6B10", "Tarca_556_P6D10",
"Tarca_557_P6E10", "Tarca_558_P6F10", "Tarca_559_P6G10", "Tarca_560_P6H10",
"Tarca_561_P6A11", "Tarca_562_P6B11", "Tarca_565_P6E11", "Tarca_566_P6F11",
"Tarca_567_P6G11", "Tarca_568_P6H11", "Tarca_569_P6A12", "Tarca_570_P6B12",
"Tarca_576_P6H12", "Tarca_578_P7B01", "Tarca_579_P7C01", "Tarca_580_P7D01",
"Tarca_581_P7E01", "Tarca_582_P7F01", "Tarca_583_P7G01", "Tarca_584_P7H01_2",
"Tarca_585_P7A02", "Tarca_586_P7B02", "Tarca_587_P7C02", "Tarca_588_P7D02",
"Tarca_589_P7E02", "Tarca_590_P7F02", "Tarca_591_P7G02", "Tarca_592_P7H02",
"Tarca_593_P7A03", "Tarca_594_P7B03", "Tarca_595_P7C03", "Tarca_597_P7E03",
"Tarca_598_P7F03", "Tarca_599_P7G03", "Tarca_600_P7H03", "Tarca_601_P7A04",
"Tarca_605_P7E04", "Tarca_606_P7F04", "Tarca_607_P7G04", "Tarca_609_P7A05",
"Tarca_611_P7C05", "Tarca_612_P7D05", "Tarca_613_P7E05", "Tarca_614_P7F05",
"Tarca_615_P7G05", "Tarca_616_P7H05", "Tarca_617_P7A06", "Tarca_618_P7B06",
"Tarca_619_P7C06", "Tarca_620_P7D06", "Tarca_621_P7E06", "Tarca_622_P7F06",
"Tarca_623_P7G06", "Tarca_624_P7H06", "Tarca_625_P7A07", "Tarca_626_P7B07",
"Tarca_628_P7D07", "Tarca_629_P7E07", "Tarca_630_P7F07", "Tarca_631_P7G07",
"Tarca_632_P7H07", "Tarca_633_P7A08", "Tarca_634_P7B08", "Tarca_635_P7C08",
"Tarca_636_P7D08", "Tarca_637_P7E08", "Tarca_638_P7F08", "Tarca_639_P7G08",
"Tarca_640_P7H08", "Tarca_641_P7A09", "Tarca_642_P7B09", "Tarca_643_P7C09",
"Tarca_646_P7F09", "Tarca_647_P7G09", "Tarca_648_P7H09_2", "Tarca_649_P7A10",
"Tarca_650_P7B10", "Tarca_651_P7C10", "Tarca_652_P7D10", "Tarca_653_P7E10",
"Tarca_654_P7F10", "Tarca_655_P7G10", "Tarca_656_P7H10", "Tarca_659_P7C11",
"Tarca_660_P7D11", "Tarca_663_P7G11", "Tarca_664_P7H11", "Tarca_665_P7A12",
"Tarca_666_P7B12", "Tarca_667_P7C12", "Tarca_668_P7D12", "Tarca_669_P7E12",
"Tarca_670_P7F12", "Tarca_671_P7G12", "Tarca_672_P7H12", "Tarca_674_P8B01",
"Tarca_675_P8C01", "Tarca_676_P8D01", "Tarca_677_P8E01", "Tarca_678_P8F01",
"Tarca_682_P8B02", "Tarca_683_P8C02", "Tarca_684_P8D02", "Tarca_688_P8H02",
"Tarca_689_P8A03", "Tarca_690_P8B03", "Tarca_692_P8D03", "Tarca_698_P8B04",
"Tarca_699_P8C04", "Tarca_700_P8D04", "Tarca_701_P8E04", "Tarca_702_P8F04",
"Tarca_703_P8G04", "Tarca_704_P8H04", "Tarca_707_P8C05", "Tarca_709_P8E05",
"Tarca_712_P8H05", "Tarca_713_P8A06", "Tarca_714_P8B06", "Tarca_715_P8C06",
"Tarca_716_P8D06", "Tarca_717_P8E06", "Tarca_718_P8F06", "Tarca_719_P8G06",
"Tarca_720_P8H06", "Tarca_721_P8A07", "Tarca_722_P8B07", "Tarca_723_P8C07",
"Tarca_726_P8F07", "Tarca_727_P8G07", "Tarca_728_P8H07", "Tarca_730_P8B08",
"Tarca_731_P8C08", "Tarca_732_P8D08", "Tarca_733_P8E08", "Tarca_734_P8F08",
"Tarca_736_P8H08", "Tarca_738_P8B09", "Tarca_739_P8C09", "Tarca_740_P8D09",
"Tarca_741_P8E09", "Tarca_742_P8F09", "Tarca_743_P8G09", "Tarca_744_P08H09",
"Tarca_745_P08A10", "Tarca_746_P8B10", "Tarca_749_P8E10", "Tarca_750_P8F10",
"Tarca_751_P8G10", "Tarca_752_P8H10", "Tarca_754_P8B11", "Tarca_755_P8C11",
"Tarca_756_P8D11", "Tarca_759_P8G11", "Tarca_760_P8H11", "Tarca_762_P08B12",
"Tarca_763_P8C12", "Tarca_764_P8D12", "Tarca_765_P8E12", "Tarca_766_P8F12",
"Tarca_768_P8H12", "Tarca_769_P9A01", "Tarca_770_P9B01", "Tarca_771_P9C01",
"Tarca_773_P9E01", "Tarca_774_P9F01", "Tarca_775_P9G01", "Tarca_777_P9A02",
"Tarca_778_P9B02", "Tarca_782_P9F02", "Tarca_784_P9H02", "Tarca_785_P9A03",
"Tarca_786_P9B03", "Tarca_787_P9C03", "Tarca_789_P9E03", "Tarca_790_P9F03",
"Tarca_791_P9G03", "Tarca_793_P9A04", "Tarca_794_P9B04", "Tarca_795_P9C04",
"Tarca_797_P9E04", "Tarca_798_P9F04", "Tarca_799_P9G04", "Tarca_800_P9H04",
"Tarca_801_P9A05", "Tarca_802_P9B05", "Tarca_803_P9C05", "Tarca_806_P9F05",
"Tarca_807_P9G05", "Tarca_808_P9H05", "Tarca_809_P9A06", "Tarca_810_P9B06",
"Tarca_811_P9C06", "Tarca_812_P9D06", "Tarca_813_P9E06", "Tarca_814_P9F06",
"Tarca_815_P9G06", "Tarca_816_P9H06", "Tarca_817_P9A07", "Tarca_818_P9B07",
"Tarca_820_P9D07", "Tarca_821_P9E07", "Tarca_822_P9F07", "Tarca_824_P9H07",
"Tarca_826_P9B08", "Tarca_827_P9C08", "Tarca_828_P9D08", "Tarca_829_P9E08",
"Tarca_830_P9F08", "Tarca_831_P9G08", "Tarca_832_P9H08", "Tarca_834_P9B09",
"Tarca_835_P9C09", "Tarca_836_P9D09", "Tarca_837_P9E09", "Tarca_838_P9F09",
"Tarca_839_P9G09", "Tarca_841_P8A10", "Tarca_842_P8B10", "Tarca_843_P8C10",
"Tarca_844_P8D10", "Tarca_845_P8E10", "Tarca_847_P8G10", "Tarca_849_P8A11",
"Tarca_850_P8B11", "Tarca_851_P8C11", "Tarca_852_P8D11", "Tarca_855_P8G11",
"Tarca_856_P8H11", "Tarca_857_P8A12", "Tarca_858_P8B12", "Tarca_859_P8C12",
"Tarca_860_P8D12_3", "Tarca_861_P8E12", "Tarca_863_P8G12", "Tarca_864_P8H12",
"Tarca_866_P10B01", "Tarca_867_P10C01", "Tarca_868_P10D01", "Tarca_869_P10E01",
"Tarca_870_P10F01", "Tarca_874_P10B02", "Tarca_875_P10C02", "Tarca_876_P10D02",
"Tarca_878_P10F02", "Tarca_879_P10G02", "Tarca_880_P10H02", "Tarca_881_P10A03",
"Tarca_884_P10D03", "Tarca_885_P10E03", "Tarca_886_P10F03", "Tarca_888_P10H03",
"Tarca_890_P10B04", "Tarca_891_P10C04", "Tarca_892_P10D04", "Tarca_893_P10E04",
"Tarca_895_P10G04", "Tarca_896_P10H04", "Tarca_897_P10A05", "Tarca_899_P10C05",
"Tarca_900_P10D05", "Tarca_903_P10G05", "Tarca_904_P10H05", "Tarca_905_P10A06",
"Tarca_906_P10B06", "Tarca_907_P10C06", "Tarca_908_P10D06", "Tarca_909_P10E06",
"Tarca_910_P10F06", "Tarca_911_P10G06", "Tarca_912_P10H06", "Tarca_913_P10A07",
"Tarca_914_P10B07", "Tarca_915_P10C07", "Tarca_916_P10D07", "Tarca_917_P10E07",
"Tarca_918_P10F07", "Tarca_919_P10G07", "Tarca_920_P10H07"), class = "factor"),
GA = c(11, 15.3, 21.7, 26.7, 31.3, 32.1, 19.7, 23.6, 27.6,
30.6, 32.6, 12.6, 18.6, 25.6, 30.6), Batch = c(1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L), Set = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("GSE113966",
"PRB_HTA"), class = "factor"), Train = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Platform = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "HTA20", class = "factor"),
`1_at` = c(6.06221469449721, 6.12502298793819, 5.8755020052495,
6.12613148162098, 6.146466258537, 6.1345548976595, 6.28953417729806,
6.32099748158838, 6.1855043077415, 6.08561779473768, 6.15856026391669,
6.01545219066942, 6.25857984382111, 6.36882457350296, 6.22016811759586
), `10_at` = c(3.79648446367096, 3.8053045883833, 3.45024474095539,
3.62841140410044, 3.44681241591488, 3.51232455992681, 3.56819306931016,
3.58462249988614, 3.6962578620003, 3.54911765491621, 3.72853662804593,
3.45867791579279, 3.59024881523945, 3.82181133052338, 3.69553021972333
), `100_at` = c(5.84933778267459, 6.19156161924549, 6.55052475296263,
6.42187743053935, 6.26096240773642, 6.15489279092855, 6.34807354206396,
6.17574188244964, 6.1797131132904, 6.11780116002087, 6.20323373624061,
6.1848681809123, 6.24635169763079, 6.28175898350025, 6.25479583503303
), `1000_at` = c(3.5677794435745, 3.45252405345375, 3.31613364795286,
3.43245075704917, 3.47716215037972, 3.63813996294905, 3.39904385276621,
3.78739437083121, 3.45201439858256, 3.54214650423219, 3.53369876966333,
3.44895159550441, 3.51532853598111, 3.54203947069514, 3.50451431462302
), `10000_at` = c(6.16681461038468, 5.67837335902992, 6.18505928400759,
5.6337568741831, 5.31319800115, 5.14814946571171, 5.64064316609978,
5.27090621047048, 5.69462506828168, 6.25755205471611, 5.7482549816934,
5.72613754557569, 5.68110995701518, 5.25478756786505, 5.14171528059565
), `100009613_at` = c(4.44302662142323, 4.77319948223297,
4.3934877055859, 4.6237834519809, 4.42265091096155, 4.66743523288194,
4.97483476597509, 4.73415116097344, 4.62910041308852, 4.78673497541689,
4.59759663067063, 4.62453903712286, 4.77791032146269, 4.79471989245065,
4.64089637146557), `100009676_at` = c(5.83652223195279, 6.14339793828768,
5.89836406552412, 6.01979203584278, 6.40769927365462, 5.98400432133011,
6.1149144301085, 6.05331581275033, 6.06510349557878, 5.74573650612351,
5.98719369609302, 6.0074614887814, 6.04564052289621, 6.12939953093625,
6.10594091413241), `10001_at` = c(6.33001755606083, 5.60174509461169,
6.13798360106589, 5.78750241567476, 5.83043737364811, 5.5920698678248,
5.84077907831575, 5.67929642825133, 5.96981067663553, 6.19490161026853,
5.75603515793641, 5.87493644012563, 5.80941714030283, 5.55586165971229,
5.80320733931781), `10002_at` = c(4.92233877299356, 4.71176546418277,
4.62812370798939, 4.79628294150335, 4.72648756689738, 4.79729686531453,
4.91913790102029, 4.88163201331439, 4.78507064150637, 4.79997095951811,
4.73932005879352, 4.85697540024369, 4.90838062744781, 4.86898558320241,
4.73415922096939), `10003_at` = c(2.68934375273141, 2.77100972258672,
2.55675627493564, 2.61341541015611, 2.63187842412618, 2.69430042092269,
2.73207812554522, 2.69169494424212, 2.63508499699814, 2.65268941561582,
2.78546450421772, 2.78129834839264, 2.66697993437978, 2.6990956336608,
2.59784138580729), `100033411_at` = c(2.74561888109989, 3.26357660188778,
2.70765553292035, 2.80774129091983, 2.76338705394557, 2.8653583834812,
3.00137677271996, 3.03783869301541, 3.084972389632, 2.83262780533507,
2.86173816184281, 2.99607734866458, 2.85563184073152, 3.13196264891025,
2.9364732038239), `100033413_at` = c(2.76060893169324, 2.40289892571191,
3.03645581534102, 2.64583376265592, 3.10581884436122, 3.24800269901788,
2.62090678070501, 2.7912327711341, 2.7222692512663, 3.40648642432304,
3.53308023141906, 2.59532220284315, 2.3166708613396, 2.66098456839415,
2.62819739311836), `100033414_at` = c(3.79468365910661, 3.34649962857668,
4.29971184424969, 3.81085169542991, 3.75804439679994, 3.81895258294878,
4.03594900960396, 3.72128838403612, 3.08893951742897, 3.82989979044012,
3.00837233455368, 3.80555080463427, 3.29585327836005, 3.18666418416018,
3.27434364943932), `100033418_at` = c(2.84818282222582, 2.29546377029219,
2.48325694938049, 3.2386968734862, 2.50164870297228, 2.72080210986981,
2.58058159047299, 2.38231438864918, 2.53998515102438, 2.53965338068817,
3.53116849547884, 2.58851750945952, 2.1940368933459, 2.34397350375656,
2.39335155022896)), row.names = c("Tarca_001_P1A01", "Tarca_003_P1A03",
"Tarca_004_P1A04", "Tarca_005_P1A05", "Tarca_006_P1A06", "Tarca_007_P1A07",
"Tarca_008_P1A08", "Tarca_011_P1A11", "Tarca_012_P1A12", "Tarca_013_P1B01",
"Tarca_015_P1B03", "Tarca_016_P1B04", "Tarca_017_P1B05", "Tarca_018_P1B06",
"Tarca_019_P1B07"), class = "data.frame")
my attempt:
pca <- prcomp(my_df[, -c(1:6)])
dat <- cbind(my_df[, c(1:6)], pca$x[, 1:5])
library(plotly)
library(dplyr)
library(RColorBrewer)
age_group = 10
dat %>%
mutate(group = paste(Batch, Platform, "age",
floor(GA/age_group)*age_group, "-",
floor(GA/age_group)*age_group + age_group - 1)) %>%
plot_ly(x = ~GA, y = ~Set, z = ~Platform, color = ~group) %>%
add_markers(marker = list(size = 2,
color = colorRampPalette(brewer.pal(11,"Spectral"))(10))) %>%
layout(scene = list(xaxis = list(title = "GA"),
yaxis = list(title = "Set"),
zaxis = list(title = "Platform")))
desired plot:
I am expecting 3D scatter plot something like this:
how can I get 3d scatter plot like that by continuing my above attempt? any idea to get this done in R? thanks
new update:
I used #TobiO solution by using actual data (I added manual colors from this thread) but seems it is pretty hard to understand for me. How can we optimize the plot?
I also once tried many different things to achieve a similar plot as in your picture. I found rgl and the car libraries most suitable.
How about this:
dat=dat %>% #make grouping part of data frame
mutate(group = paste(Batch, Platform, "age",
floor(GA/age_group)*age_group, "-",
floor(GA/age_group)*age_group + age_group - 1) %>% factor)
dat$colors=color=brewer.pal(length(levels(dat$group)),"Paired")[as.numeric(dat$group)] #add colors (in your call, there seem too few colors defined)
mycolorscheme=unique(dat$colors)
names(mycolorscheme)=unique(dat$group) #have this as a named vector
library(car)
library(rgl)
open3d()
scatter3d(x = dat$PC1, y = dat$PC2, z = dat$PC3,
groups = dat$group,
grid = T, surface = F,ellipsoid = T,surface.col =mycolorscheme,
xlab="PC1",ylab="PC2",zlab="PC3")
texts3d(x = dat$PC1, y = dat$PC2, z = dat$PC3,
groups = dat$group,texts=dat$group, pos=2,color=mycolorscheme,adj=0)
In you example dataset the groups are so small, that there are problems with drawing those ellipses, which causes trouble with positioning the text labels.
There is also an interactive version available in car::Identify3d

How to use ifelse and paste functions

I am learning the use of the ifelse function from Zuur et al (2009) A Beginners guide to R. In one exercise, there is a data frame called Owls which contains data about about 27 nests and two night of observations.
structure(list(Nest = structure(c(1L, 1L, 1L, 1L), .Label = "AutavauxTV", class = "factor"),
FoodTreatment = structure(c(1L, 2L, 1L, 1L), .Label = c("Deprived",
"Satiated"), class = "factor"), SexParent = structure(c(1L,
1L, 1L, 1L), .Label = "Male", class = "factor"), ArrivalTime = c(22.25,
22.38, 22.53, 22.56), SiblingNegotiation = c(4L, 0L, 2L,
2L), BroodSize = c(5L, 5L, 5L, 5L), NegPerChick = c(0.8,
0, 0.4, 0.4)), .Names = c("Nest", "FoodTreatment", "SexParent",
"ArrivalTime", "SiblingNegotiation", "BroodSize", "NegPerChick"
), row.names = c(NA, 4L), class = "data.frame")
The two nights differed as to the feeding regime (satiated or deprived) and are indicated in the Foodregime variable. The task is to use ifelse and past functions that make a new categorical variable that defines observations from a single night at a particular nest.
In the solutions the following code is suggested:
Owls <- read.table(file = "Owls.txt", header = TRUE, dec = ".")
ifelse(Owls$FoodTreatment == "Satiated", Owls$NestNight <- paste(Owls$Nest, "1",sep = "_"), Owls$NestNight <- paste(Owls$Nest, "2",sep = "_"))
and apparently it creates a new variable with values the endings of which vary ("-1" or "-2")
however when I call the original dataframe, all "-1" endings in the NestNight variable disappears and are turned to "-2."
Why does this happen? Did the authors miss something from the code or it's me who is not getting it?
Many thanks
EDIT: Sorry, I wanted to give a reproducible example by copying my data using dput but it did not work. If you can let me know how I can correct it so that it appears properly, I'd be grateful too!
Solution
If you do the assignment outside the ifelse structure, it works:
Owls$NestNight <- ifelse(Owls$FoodTreatment == "Satiated",
paste(Owls$Nest, "1",sep = ""),
paste(Owls$Nest, "2",sep = ""))
Explanation
What happens in your case is simply if you would execute the following two lines:
Owls$NestNight <- paste(Owls$Nest, "1",sep = "")
Owls$NestNight <- paste(Owls$Nest, "2",sep = "")
You first assign paste(Owls$Nest, "1",sep = "") to Owls$NestNight and then you reassign paste(Owls$Nest, "2",sep = "") to it. The ifelse is not affected by this, but you don't assign it's result to any variable.
Maybe it is more clear if you test this simple code:
c(a <- 1:5, a <- 6:10) #c is your ifelse, a is your Owls$NestNight
a #[1] 6 7 8 9 10

Sorting dataframe by two columns when one is selected by user

I want to sort data frame by two columns first is factor and second numeric but this is selected in shiny.
When I use this line with specified columns it works well.
iris1<-iris[with(iris,order(Species,Sepal.Length,decreasing = T)), ]
But inside shiny I replace Sepal.Length with reactive values and this returns error. Please see MRE
# ui.R
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Miles Per Gallon"),
sidebarPanel(selectInput("a","select column",c("Sepal.Length","Sepal.Width"))),
mainPanel(tableOutput("b"))
))
# server.R
library(shiny)
shinyServer(function(input, output) {
col<-reactive({input$a})
output$b<- renderTable({
########### this line probably causes the problem
iris1<-iris[with(iris,order(Species,col(),decreasing = T)), ]
})
})
Error I get is
Error in order(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, :
argument lengths differ
Try this, using get() to convert string to column name:
col <- "Sepal.Width"
iris[with(iris, order(Species, get(col), decreasing = TRUE)), ]
And within shiny it should be: get(col()).
Note that input$a is already reactive, so we can say: get(input$a)

interpolate with glmer where new data is a Raster Stack and model is fixed effects with lat and long as independent predictors

I am trying to make a raster file to predict where species will occur. The model that I want to run is:
glmer(colorSymbol ~ snow.cover_sc + bio2_sc + bio3_sc + alt_sc + y + x + (1|spsCode), family = binomial, data = data)
It is a mixed effects model with lat (x) and long (y) as independent predictors as well as various environmental covariates. x and y are geographical coordinates. Since x and y are independent predictors I am trying to use the interpolate function in the raster package. However, the help file for interpolate gives gstat (gstat package) or Krige (fields package) as example model objects.
So my first question is can a glmer serve as a model for the interpolate function? I realize that a glmer assumes independence between the predictors and tested for that to see if there was correlation between any of them and found none (<0.5).
My second question is if glmer can be the model object, what does the following error mean:
Error in model.frame.default(Terms, newdata, na.action = na.action,
xlev = object$xlevels) : object is not a matrix In addition:
Warning message: closing unused connection 4
For context I get this error when I run the reduced model:
m4<- glm(colorSymbol ~ bio_2 + alt + x + y, family = binomial, data = data)
and create a raster stack of bio_2 and alt. Links to files below: (Note they can also be accessed via worldclim(dot)org/current (sorry can't post more than 2 links) and downloading ESRI 30 second grids.)
alt raster file
bio_2 raster file
Here is the full code:
data4<-structure(list(colorSymbol = c(1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L), bio_2 =
structure(c(-1.65319533124791,
5.12773277360962, -2.96563302896227, 2.13829135103802, 1.62789891303799,
0.169634804466482, -0.049104811819245, -0.049104811819245,
-0.049104811819245,
-0.049104811819245, -0.267844428104972, 0.315461215323633,
0.315461215323633,
-0.63241045524785, -0.63241045524785, 0.315461215323633, -0.122018017247821,
-0.049104811819245, -0.413670838962123, 0.169634804466482), .Dim = c(20L,
1L)), alt = structure(c(0.751340496188818, 4.17865221830445,
-0.118372064874358, -0.554302064617135, 1.86371359898073,
0.0126216788907128,
-0.595103394642321, -0.595103394642321, -0.558596941461892,
-0.573629010418539,
-0.0840130501163067, -0.0625386658925246, 0.0620127626054117,
2.11925877124374, 2.11925877124374, -0.124814380141493, -0.543564872505244,
-0.719654823140258, 0.495795323925811, 0.20803857532713), .Dim = c(20L,
1L)), y = structure(c(0.0353970033327643, 1.83610974064461,
-4.82625744580285,
-4.36879939725431, 1.11073398331965, 0.101128667461893, 0.171636464096401,
0.171636497654332, 0.168280671013401, 0.168839981046544, 0.173873670671044,
0.10507991246954, 0.0997146033725779, 0.0106082967555351,
0.0106082967555351,
0.105639188944753, 0.182263153378545, 0.186305172589088, 0.133466968853809,
0.10507991246954), .Dim = c(20L, 1L)), x = structure(c(3.73193203850492,
-3.74207501883321, 1.93312018034606, -3.43881737052527, -1.87240343109311,
-0.289046352405738, 0.13805360014565, 0.13805360014565, 0.0955082550467424,
0.0997628661381006, 0.00616320902913118, 0.0869992881355855,
-0.236861953199331, -0.103499155724443, -0.103499410996004,
0.0912538992269437,
0.0997628661381006, 0.0498381307812604, 0.220158634177113,
0.0784903212244285
), .Dim = c(20L, 1L))), .Names = c("colorSymbol", "bio_2", "alt",
"y", "x"), row.names = c(NA, 20L), class = "data.frame")
library("lme4")
library("raster")
library("rgdal")
library("RArcInfo")
m7 = glm(colorSymbol ~ bio_2 + alt + x + y, family = binomial, data = data4)
#write model
alt<-raster("alt.ovr")
bio_2<-raster("bio_2.ovr")
#import rasters
rasstack<-stack(alt, bio_2)
#make raster stack
test<-raster::interpolate(rasstack, m7)
#try and interpolate model
Any help would be greatly appreciated!

Call variable from custom function

This would seem to be a straightforward problem but I can't find an answer for it...
How do I write a function where one of the calls refers to a specific variable name?
For example, if I have a data frame:
data=structure(list(x = 1:10, treatment = c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L)), .Names = c("x", "treatment"), row.names = c(NA,
-10L), class = "data.frame")
I can write a trivial function that adds treatment to the other variable in the data frame but this only works if there is a variable called "treatment" in d.
ff=function(data,treatment){data+treatment)}
ff(data,data$treatment)
This works but I want to set it up so the user doesn't call data$Var in the function.
Is this what you want?
ff <- function(data, colname) {
data + data[[colname]]
}
ff( data, "treatment" )
or
ff <- function(data, column) {
colname <- deparse(substitute(column))
data + data[[colname]]
}
ff( data, treatment )
(the later can lead to hard to find bugs if someone tries something like ff(data, 1:10))

Resources