The output from LPSolver is not the optimal result - r

Since I need to create an automatic shift planning tool, R lpSolve is the package I plan to use. However, I can't get the optimal output by the code shown as below. The output(hourly supply by accumulating all shifts available) can't fulfill the demand. e.g. The demand for a hour is 46 but the supply is only 25 which means 21 unit of demand can't be satisfied.
Background:
The objective
To minimize the difference between total supply and total demand.
To minimize the difference between supply and demand for each hour.
Constraint
Shift constraint - each hour might have several shifts available to be assigned.
Max capacity - I have the cap of supply. the sum of total shift cant exceed the cap for each hour. (46 is the cap in this example).
In the constr matrix, 24 rows represent 24 hours starting from 7 am and 9 columns refers to no. of shifts I have.
The constr.val refers to hourly demand.
Please let me know if there is anything unclear. thanks!
library(lpSolve)
obj.fun<-c(1,1,1,1,1,1,1,1,1)
constr<-matrix(c(
1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0,
1, 0, 0, 1, 0, 1, 0, 0, 0,
1, 0, 0, 1, 0, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 0, 1, 1, 0, 1, 0,
0, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 0, 0, 1, 0, 1, 0, 1,
0, 1, 0, 0, 1, 0, 1, 0, 1,
0, 1, 0, 0, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 0, 1, 0, 1,
0, 0, 1, 0, 0, 0, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0), nrow = 24, byrow = TRUE)
constr.dir <- rep("<=", 24)
constr.val <- c(24, 20, 21, 22, 26, 34, 40, 44,
46, 46, 46, 46, 46, 46, 46, 46,
46, 46, 46, 46, 46, 46, 41, 27)
day.shift <- lp("max", obj.fun, constr, constr.dir,
constr.val, compute.sens = TRUE)
day.shift$solution

Related

How to prevent two NPCs from damaging each other with their AoE spells in Azerothcore?

I am running a local server of Azerothcore with rev. 3716ddf3e4e1 2021-05-25 18:05:13 +0200 (master branch).
I have created two custom NPCs and added them to my DB with the following code:
INSERT INTO `creature_template` (`entry`, `difficulty_entry_1`, `difficulty_entry_2`, `difficulty_entry_3`, `KillCredit1`, `KillCredit2`, `modelid1`, `modelid2`, `modelid3`, `modelid4`, `name`, `subname`, `gossip_menu_id`, `minlevel`, `maxlevel`, `exp`, `faction`, `npcflag`, `speed_walk`, `speed_run`, `scale`, `rank`, `dmgschool`, `DamageModifier`, `BaseAttackTime`, `RangeAttackTime`, `BaseVariance`, `RangeVariance`, `unit_class`, `unit_flags`, `unit_flags2`, `dynamicflags`, `family`, `trainer_type`, `trainer_spell`, `trainer_class`, `trainer_race`, `type`, `type_flags`, `lootid`, `pickpocketloot`, `skinloot`, `PetSpellDataId`, `VehicleId`, `mingold`, `maxgold`, `AIName`, `MovementType`, `InhabitType`, `HoverHeight`, `HealthModifier`, `ManaModifier`, `ArmorModifier`, `RacialLeader`, `movementId`, `RegenHealth`, `mechanic_immune_mask`, `spell_school_immune_mask`, `flags_extra`, `ScriptName`, `VerifiedBuild`) VALUES
(1112001, 0, 0, 0, 0, 0, 3456, 0, 0, 0, 'Mob1', '', 0, 43, 43, 0, 63, 0, 1, 1.14286, 3, 3, 0, 30, 2000, 2000, 1, 1, 1, 32832, 2048, 0, 0, 0, 0, 0, 0, 7, 4, 0, 0, 0, 0, 0, 50000, 60000, 'SmartAI', 1, 3, 1, 100, 1, 1, 0, 0, 1, 0, 0, 256, '', 12340),
(1112003, 0, 0, 0, 0, 0, 21443, 0, 0, 0, 'Mob2', '', 0, 42, 42, 0, 63, 0, 1, 1.14286, 1, 1, 0, 10, 2000, 2000, 1, 1, 1, 0, 2048, 0, 0, 0, 0, 0, 0, 6, 0, 4860, 0, 0, 0, 0, 297, 393, 'SmartAI', 1, 3, 1, 60, 1, 1, 0, 0, 1, 650854271, 0, 0, '', 12340);
Once i let Mob1 cast spell Death and Decay, Mob2 will take damage from it once they walk across it. How can i prevent the mobs from damaging each other, but only do damage to players instead?
Have you tried to set the two NPC to an allied faction that is also an enemy faction for the player ?

How to use ids from one dataframe to sum rows in another dataframe

I feel like this answer has been asked before, but I can't seem to find an answer to this question. Maybe my title is too vague, so feel free to change it.
So I have one data frame, a, with ids the correspond to column name in data frame b. Both data frames are simplified versions of a much larger data frame.
here is data frame a
a <- structure(list(V1 = structure(c(4L, 5L, 1L, 2L, 3L), .Label = c("GEN[D00105].GT",
"GEN[D00151].GT", "GEN[D00188].GT", "GEN[D86396].GT", "GEN[D86397].GT"
), class = "factor")), row.names = c(NA, -5L), class = "data.frame")
here is data frame b
b <- structure(list(`GEN[D01104].GT` = c(0, 0, 0, 0, 1, 0, 0, 2, 0,
1, 1, 1, 1, 0, 0, 0, 2, 0, 0, 0), `GEN[D01312].GT` = c(1, 0,
2, 2, 0, 0, 0, 0, 0, 1, 1, 0, 0, 2, 0, 0, 2, 0, 0, 0), `GEN[D01878].GT` = c(0,
0, 0, 2, 0, 0, 2, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0), `GEN[D01882].GT` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2, 0, 0, 0, 0), `GEN[D01952].GT` = c(0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0), `GEN[D01953].GT` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2, 0, 0, 0, 2, 0), `GEN[D02053].GT` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0), `GEN[D00316].GT` = c(0,
0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 2, 0, 0), `GEN[D01827].GT` = c(0,
0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0), `GEN[D01881].GT` = c(0,
0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0), `GEN[D02044].GT` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0), `GEN[D02085].GT` = c(0,
0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0), `GEN[D02204].GT` = c(0,
0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0), `GEN[D02276].GT` = c(0,
0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0), `GEN[D02297].GT` = c(0,
0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0), `GEN[D02335].GT` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0), `GEN[D02397].GT` = c(0,
0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0), `GEN[D00856].GT` = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0), `GEN[D00426].GT` = c(0,
0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0), `GEN[D02139].GT` = c(0,
0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0), `GEN[D02168].GT` = c(0,
0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0)), row.names = c(NA,
-20L), class = "data.frame")
I want to be able to use the ids from data frame a to sum the row in data frame b that have a matching id if that makes sense.
So in the past, I just did something like
b$affected.samples <- (b$`GEN[D86396].GT` + b$`GEN[D86397].GT` + b$`GEN[D00105].GT` + b$`GEN[D00151].GT` + b$`GEN[D00188].GT`)
which got annoying and took to much time, so I moved over to
b$affected.samples <- rowSums(b[,c(1:5)])
Which isn't too bad for this example but with my large data set, my sample can be all over the place, and it's starting to take too much time to finds where everything is. I was hoping there is a way just to use my data frame a to sum the correct rows in data frame b.
Hopefully, I gave this is all the information you need! Let me know if you have any questions.
Thanks in advance!!
Extract the 'V1' column as a character string, use that to select the columns of 'b' (assuming these column names are found in 'b') and get the rowSums
rowSums( b[as.character(a$V1)], na.rm = TRUE)

tapply of two variables in a loop

My data :
TEST <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1,
0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 3, 1, 0, 0,
0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 1, 1, 1,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2,
0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0,
1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0,
0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
3, 0, 0, 2, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0,
0, 0, 2, 0, 1, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 3, 0, 0, 1, 0, 0, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 1, 0, 0, 2, 0, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 0, 0, 1,
0, 0, 0), .Dim = c(22L, 20L), .Dimnames = list(NULL, c("month",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "")))
and :
month <- c(1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2)
I want to sum by row, so I used this code :
su_test <- list()
for (i in 1:ncol(TEST)){
su_test[[i]] <- tapply(TEST[,i], month, sum)
}
su_test <- do.call(cbind, su_test)
and to check the quantile :
su_test_obs <- apply(su_test,1,quantile,c(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 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 3, 0, 0, 1, 0, 0, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 1, 0, 0, 2, 0, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 1), .Dim = c(22L, 20L), .Dimnames = list(NULL, c("month",
"area", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "")))
area <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
I would like to have the same result of su_test_obs
but with the details of the areas in addition like list(month, area) but I don't understand the logic.
Would you have a solution please? Maybe there is a solution simpler with dplyr?
Thanks
It would be much simpler if you convert your matrix to dataframe. We can then use aggregate which can be applied to multiple groups easily
df <- data.frame(TEST2)
apply(aggregate(.~month + area, df, sum)[-c(1, 2)], 1, quantile, c(0.1,0.9))
# [,1] [,2]
#10% 2 1.7
#90% 6 5.3

Why is auto.arima() giving me the Error: "'by' argument is much too small"

I am working on predicting intra-day sales for a retailer. We want to know if we can predict sales through the rest of the day, based off sales within that day. I'm working with roughly 3 years of data in a time series, which has given me roughly 26,000 rows of data.
I've never worked with a time series this large so my approach might be off. Or auto.arima() may not have been made to handle data this large.
I've tried limiting my data down to even 300 rows and had marginal success, but have not found anything that works with my larger data set. auto.arima() doesn't even have a by = argument from what I can find.
my_ts <- structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3,
3, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 4, 1,
1, 0, 3, 1, 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 0, 0, 0,
0, 0, 1, 0, 1, 1, 2, 0, 1, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 3, 1, 2, 2, 3, 1, 4, 4, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 4, 1, 2, 0, 0, 5, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 8, 0, 1, 1, 4, 0, 4, 3,
2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 6, 1, 0, 0, 0,
3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8,
1, 2, 0, 4, 2, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
1, 6, 7, 0, 1, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 5, 0, 2, 1, 4, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 2, 1, 4, 7, 5, 0, 7, 4, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 6, 0, 4, 6, 2, 1, 3, 5, 3, 3, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 3, 0, 1, 1, 3,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 6, 0, 0), index = structure(c(1435190400,
1435194000, 1435197600, 1435201200, 1435204800, 1435208400, 1435212000,
1435215600, 1435219200, 1435222800, 1435226400, 1435230000, 1435233600,
1435237200, 1435240800, 1435244400, 1435248000, 1435251600, 1435255200,
1435258800, 1435262400, 1435266000, 1435269600, 1435273200, 1435276800,
1435280400, 1435284000, 1435287600, 1435291200, 1435294800, 1435298400,
1435302000, 1435305600, 1435309200, 1435312800, 1435316400, 1435320000,
1435323600, 1435327200, 1435330800, 1435334400, 1435338000, 1435341600,
1435345200, 1435348800, 1435352400, 1435356000, 1435359600, 1435363200,
1435366800, 1435370400, 1435374000, 1435377600, 1435381200, 1435384800,
1435388400, 1435392000, 1435395600, 1435399200, 1435402800, 1435406400,
1435410000, 1435413600, 1435417200, 1435420800, 1435424400, 1435428000,
1435431600, 1435435200, 1435438800, 1435442400, 1435446000, 1435449600,
1435453200, 1435456800, 1435460400, 1435464000, 1435467600, 1435471200,
1435474800, 1435478400, 1435482000, 1435485600, 1435489200, 1435492800,
1435496400, 1435500000, 1435503600, 1435507200, 1435510800, 1435514400,
1435518000, 1435521600, 1435525200, 1435528800, 1435532400, 1435536000,
1435539600, 1435543200, 1435546800, 1435550400, 1435554000, 1435557600,
1435561200, 1435564800, 1435568400, 1435572000, 1435575600, 1435579200,
1435582800, 1435586400, 1435590000, 1435593600, 1435597200, 1435600800,
1435604400, 1435608000, 1435611600, 1435615200, 1435618800, 1435622400,
1435626000, 1435629600, 1435633200, 1435636800, 1435640400, 1435644000,
1435647600, 1435651200, 1435654800, 1435658400, 1435662000, 1435665600,
1435669200, 1435672800, 1435676400, 1435680000, 1435683600, 1435687200,
1435690800, 1435694400, 1435698000, 1435701600, 1435705200, 1435708800,
1435712400, 1435716000, 1435719600, 1435723200, 1435726800, 1435730400,
1435734000, 1435737600, 1435741200, 1435744800, 1435748400, 1435752000,
1435755600, 1435759200, 1435762800, 1435766400, 1435770000, 1435773600,
1435777200, 1435780800, 1435784400, 1435788000, 1435791600, 1435795200,
1435798800, 1435802400, 1435806000, 1435809600, 1435813200, 1435816800,
1435820400, 1435824000, 1435827600, 1435831200, 1435834800, 1435838400,
1435842000, 1435845600, 1435849200, 1435852800, 1435856400, 1435860000,
1435863600, 1435867200, 1435870800, 1435874400, 1435878000, 1435881600,
1435885200, 1435888800, 1435892400, 1435896000, 1435899600, 1435903200,
1435906800, 1435910400, 1435914000, 1435917600, 1435921200, 1435924800,
1435928400, 1435932000, 1435935600, 1435939200, 1435942800, 1435946400,
1435950000, 1435953600, 1435957200, 1435960800, 1435964400, 1435968000,
1435971600, 1435975200, 1435978800, 1435982400, 1435986000, 1435989600,
1435993200, 1435996800, 1436000400, 1436004000, 1436007600, 1436011200,
1436014800, 1436018400, 1436022000, 1436025600, 1436029200, 1436032800,
1436036400, 1436040000, 1436043600, 1436047200, 1436050800, 1436054400,
1436058000, 1436061600, 1436065200, 1436068800, 1436072400, 1436076000,
1436079600, 1436083200, 1436086800, 1436090400, 1436094000, 1436097600,
1436101200, 1436104800, 1436108400, 1436112000, 1436115600, 1436119200,
1436122800, 1436126400, 1436130000, 1436133600, 1436137200, 1436140800,
1436144400, 1436148000, 1436151600, 1436155200, 1436158800, 1436162400,
1436166000, 1436169600, 1436173200, 1436176800, 1436180400, 1436184000,
1436187600, 1436191200, 1436194800, 1436198400, 1436202000, 1436205600,
1436209200, 1436212800, 1436216400, 1436220000, 1436223600, 1436227200,
1436230800, 1436234400, 1436238000, 1436241600, 1436245200, 1436248800,
1436252400, 1436256000, 1436259600, 1436263200, 1436266800, 1436270400,
1436274000, 1436277600, 1436281200, 1436284800, 1436288400, 1436292000,
1436295600, 1436299200, 1436302800, 1436306400, 1436310000, 1436313600,
1436317200, 1436320800, 1436324400, 1436328000, 1436331600, 1436335200,
1436338800, 1436342400, 1436346000, 1436349600, 1436353200, 1436356800,
1436360400, 1436364000, 1436367600, 1436371200, 1436374800, 1436378400,
1436382000, 1436385600, 1436389200, 1436392800, 1436396400, 1436400000,
1436403600, 1436407200, 1436410800, 1436414400, 1436418000, 1436421600,
1436425200, 1436428800, 1436432400, 1436436000, 1436439600, 1436443200,
1436446800, 1436450400, 1436454000, 1436457600, 1436461200, 1436464800,
1436468400, 1436472000, 1436475600, 1436479200, 1436482800, 1436486400,
1436490000, 1436493600, 1436497200, 1436500800, 1436504400, 1436508000,
1436511600, 1436515200, 1436518800, 1436522400, 1436526000, 1436529600,
1436533200, 1436536800, 1436540400, 1436544000, 1436547600, 1436551200,
1436554800, 1436558400, 1436562000, 1436565600, 1436569200, 1436572800,
1436576400, 1436580000, 1436583600, 1436587200, 1436590800, 1436594400,
1436598000, 1436601600, 1436605200, 1436608800, 1436612400, 1436616000,
1436619600, 1436623200, 1436626800, 1436630400, 1436634000, 1436637600,
1436641200, 1436644800, 1436648400, 1436652000, 1436655600, 1436659200,
1436662800, 1436666400, 1436670000, 1436673600, 1436677200, 1436680800,
1436684400, 1436688000, 1436691600, 1436695200, 1436698800, 1436702400,
1436706000, 1436709600, 1436713200, 1436716800, 1436720400, 1436724000,
1436727600, 1436731200, 1436734800, 1436738400, 1436742000, 1436745600,
1436749200, 1436752800, 1436756400, 1436760000, 1436763600, 1436767200,
1436770800, 1436774400, 1436778000, 1436781600, 1436785200, 1436788800,
1436792400, 1436796000, 1436799600, 1436803200, 1436806800, 1436810400,
1436814000, 1436817600, 1436821200, 1436824800, 1436828400, 1436832000,
1436835600, 1436839200, 1436842800, 1436846400, 1436850000, 1436853600,
1436857200, 1436860800, 1436864400, 1436868000, 1436871600, 1436875200,
1436878800, 1436882400, 1436886000, 1436889600, 1436893200, 1436896800,
1436900400, 1436904000, 1436907600, 1436911200, 1436914800, 1436918400,
1436922000, 1436925600, 1436929200, 1436932800, 1436936400, 1436940000,
1436943600, 1436947200, 1436950800, 1436954400, 1436958000, 1436961600,
1436965200, 1436968800, 1436972400, 1436976000, 1436979600, 1436983200,
1436986800, 1436990400, 1436994000, 1436997600, 1437001200, 1437004800,
1437008400, 1437012000, 1437015600, 1437019200, 1437022800, 1437026400,
1437030000, 1437033600, 1437037200, 1437040800, 1437044400, 1437048000,
1437051600, 1437055200, 1437058800, 1437062400, 1437066000, 1437069600,
1437073200, 1437076800, 1437080400, 1437084000, 1437087600, 1437091200,
1437094800, 1437098400, 1437102000, 1437105600, 1437109200, 1437112800,
1437116400, 1437120000, 1437123600, 1437127200, 1437130800, 1437134400,
1437138000, 1437141600, 1437145200, 1437148800, 1437152400, 1437156000,
1437159600, 1437163200, 1437166800, 1437170400, 1437174000, 1437177600,
1437181200, 1437184800, 1437188400, 1437192000, 1437195600, 1437199200,
1437202800, 1437206400, 1437210000, 1437213600, 1437217200, 1437220800,
1437224400, 1437228000, 1437231600, 1437235200, 1437238800, 1437242400,
1437246000, 1437249600, 1437253200, 1437256800, 1437260400, 1437264000,
1437267600, 1437271200, 1437274800, 1437278400, 1437282000, 1437285600,
1437289200, 1437292800, 1437296400, 1437300000, 1437303600, 1437307200,
1437310800, 1437314400, 1437318000, 1437321600, 1437325200, 1437328800,
1437332400, 1437336000, 1437339600, 1437343200, 1437346800, 1437350400,
1437354000, 1437357600, 1437361200, 1437364800, 1437368400, 1437372000,
1437375600, 1437379200, 1437382800, 1437386400, 1437390000, 1437393600,
1437397200, 1437400800, 1437404400, 1437408000, 1437411600, 1437415200,
1437418800, 1437422400, 1437426000, 1437429600, 1437433200, 1437436800,
1437440400, 1437444000, 1437447600, 1437451200, 1437454800, 1437458400,
1437462000, 1437465600, 1437469200, 1437472800, 1437476400, 1437480000,
1437483600, 1437487200, 1437490800, 1437494400, 1437498000, 1437501600,
1437505200, 1437508800, 1437512400, 1437516000, 1437519600, 1437523200,
1437526800, 1437530400, 1437534000, 1437537600, 1437541200, 1437544800,
1437548400, 1437552000, 1437555600, 1437559200, 1437562800, 1437566400,
1437570000, 1437573600, 1437577200, 1437580800, 1437584400, 1437588000,
1437591600, 1437595200, 1437598800, 1437602400, 1437606000, 1437609600,
1437613200, 1437616800, 1437620400, 1437624000, 1437627600, 1437631200,
1437634800, 1437638400, 1437642000, 1437645600, 1437649200, 1437652800,
1437656400, 1437660000, 1437663600, 1437667200, 1437670800, 1437674400,
1437678000, 1437681600, 1437685200, 1437688800, 1437692400, 1437696000,
1437699600, 1437703200, 1437706800, 1437710400, 1437714000, 1437717600,
1437721200, 1437724800, 1437728400, 1437732000, 1437735600, 1437739200,
1437742800, 1437746400, 1437750000, 1437753600, 1437757200, 1437760800,
1437764400, 1437768000, 1437771600, 1437775200, 1437778800, 1437782400,
1437786000, 1437789600, 1437793200, 1437796800, 1437800400, 1437804000,
1437807600, 1437811200, 1437814800, 1437818400, 1437822000, 1437825600,
1437829200, 1437832800, 1437836400, 1437840000, 1437843600, 1437847200,
1437850800, 1437854400, 1437858000, 1437861600, 1437865200, 1437868800,
1437872400, 1437876000, 1437879600, 1437883200, 1437886800, 1437890400,
1437894000, 1437897600, 1437901200, 1437904800, 1437908400, 1437912000,
1437915600, 1437919200, 1437922800, 1437926400, 1437930000, 1437933600,
1437937200, 1437940800, 1437944400, 1437948000, 1437951600, 1437955200,
1437958800, 1437962400, 1437966000, 1437969600, 1437973200, 1437976800,
1437980400, 1437984000, 1437987600, 1437991200, 1437994800, 1437998400,
1438002000, 1438005600, 1438009200, 1438012800, 1438016400, 1438020000,
1438023600, 1438027200, 1438030800, 1438034400, 1438038000, 1438041600,
1438045200, 1438048800, 1438052400, 1438056000, 1438059600, 1438063200,
1438066800, 1438070400, 1438074000, 1438077600, 1438081200, 1438084800,
1438088400, 1438092000, 1438095600, 1438099200, 1438102800, 1438106400,
1438110000, 1438113600, 1438117200, 1438120800, 1438124400, 1438128000,
1438131600, 1438135200, 1438138800, 1438142400, 1438146000, 1438149600,
1438153200, 1438156800, 1438160400, 1438164000, 1438167600, 1438171200,
1438174800, 1438178400, 1438182000, 1438185600, 1438189200, 1438192800,
1438196400, 1438200000, 1438203600, 1438207200, 1438210800, 1438214400,
1438218000, 1438221600, 1438225200, 1438228800, 1438232400, 1438236000,
1438239600, 1438243200, 1438246800, 1438250400, 1438254000, 1438257600,
1438261200, 1438264800, 1438268400, 1438272000, 1438275600, 1438279200,
1438282800, 1438286400, 1438290000, 1438293600, 1438297200, 1438300800,
1438304400, 1438308000, 1438311600, 1438315200, 1438318800, 1438322400,
1438326000, 1438329600, 1438333200, 1438336800, 1438340400, 1438344000,
1438347600, 1438351200, 1438354800, 1438358400, 1438362000, 1438365600,
1438369200, 1438372800, 1438376400, 1438380000, 1438383600, 1438387200,
1438390800, 1438394400, 1438398000, 1438401600, 1438405200, 1438408800,
1438412400, 1438416000, 1438419600, 1438423200, 1438426800, 1438430400,
1438434000, 1438437600, 1438441200, 1438444800, 1438448400, 1438452000,
1438455600, 1438459200, 1438462800, 1438466400, 1438470000, 1438473600,
1438477200, 1438480800, 1438484400, 1438488000, 1438491600, 1438495200,
1438498800, 1438502400, 1438506000, 1438509600, 1438513200, 1438516800,
1438520400, 1438524000, 1438527600, 1438531200, 1438534800, 1438538400,
1438542000, 1438545600, 1438549200, 1438552800, 1438556400, 1438560000,
1438563600, 1438567200, 1438570800, 1438574400, 1438578000, 1438581600,
1438585200, 1438588800, 1438592400, 1438596000, 1438599600, 1438603200,
1438606800, 1438610400, 1438614000, 1438617600, 1438621200, 1438624800,
1438628400, 1438632000, 1438635600, 1438639200, 1438642800, 1438646400,
1438650000, 1438653600, 1438657200, 1438660800, 1438664400, 1438668000,
1438671600, 1438675200, 1438678800, 1438682400, 1438686000, 1438689600,
1438693200, 1438696800, 1438700400, 1438704000, 1438707600, 1438711200,
1438714800, 1438718400, 1438722000, 1438725600, 1438729200, 1438732800,
1438736400, 1438740000, 1438743600, 1438747200, 1438750800, 1438754400,
1438758000, 1438761600, 1438765200, 1438768800, 1438772400, 1438776000,
1438779600, 1438783200, 1438786800), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), class = c("zooreg", "zoo"), frequency = 24)
fit1 <-auto.arima(my_ts,seasonal = TRUE)
I was hoping to get a model through arima, but I'm only getting the error:
"Error in seq.default(head(tt, 1), tail(tt, 1), deltat) :
'by' argument is much too small"

Stacked barplot with partitioned main and sub x label

I'm new to R. I want to create a stacked barplot based on the following data. I want to plot these data into a stacked barplot that describes percentage of disease score for each combination of genotype and race in x axis, e.g. 76R-race 1, rmc-race 1. As well how to simplify the x axis, by splitting each race to have 76R and rmc combination instead of labelling each combination, i.e. instead of labelling each bar with 76R-race 1, rmc-race 1, etc, how to label race 1 as main axis and have 76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, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
genotype=gl(2,52,624, labels=c("76R","rmc")),
race=gl(6,104,624, labels=c("race 1","race 2","race 3","WAC 7673","WAC 7591","control")))
I'm not sure if this is what you're looking for:
library(ggplot2)
library(scales)
ggplot(disease.nov, aes(x=genotype, fill=factor(disease.score))) +
geom_bar(position="fill") +
facet_wrap(~ race) +
scale_y_continuous(labels = percent_format())
Which produces:

Resources