For the portfolio optimization model, we need a balanced panel for the variety by planting date combination over the 4 years. We therefore subset only locations, varieties and planting dates that meet this criteria
weightsPlot(longFrontier_AllBihar_small, mtext =FALSE)text <-"Mean-Variance Portfolio - Long Only Constraints"mtext(text, side =3, line =3, font =2, cex =0.9)
col =divPalette(15, "RdYlGn")weightsPlot(covOGKFrontier, col = col, mtext =FALSE)text <-"OGK Robustified MV Portfolio"mtext(text, side =3, line =3, font =2, cex =0.9)
weightedReturnsPlot(covOGKFrontier, col = col, mtext =FALSE)
covRiskBudgetsPlot(covOGKFrontier, col = col, mtext =FALSE)
5 Economic Optimization
# Import the cost of cultivation data CACP_Data_2008_20=import("CACP_Data_2008_20.csv") library(janitor)CACP_Data_2008_20=CACP_Data_2008_20 %>%clean_names()CACP_Data_2008_20$district_name[CACP_Data_2008_20$district_name=="Arrah"]="Ara"CACP_Data_2008_20_Bihar=subset(CACP_Data_2008_20,CACP_Data_2008_20$district_name%in%c("Ara","Begusarai","Buxar","East Champaran","Lakhisarai","Madhepura","Muzaffarpur","Rohtas") & crop=="Wheat"& year%in%c("2016-17","2017-18","2018-19","2019-20"))# Calculate prices ---------------------library(data.table)CACP_Data_2008_20_Bihar=data.table(CACP_Data_2008_20_Bihar)CACP_Data_2008_20_Bihar=na_if(CACP_Data_2008_20_Bihar, 0)library(data.table)CACP_Data_2008_20_Bihar=data.table(CACP_Data_2008_20_Bihar)#FertilizerCACP_Data_2008_20_Bihar$fertiliser_n_rs_kg=CACP_Data_2008_20_Bihar$fertiliser_n_rs/CACP_Data_2008_20_Bihar$fertiliser_n_kgCACP_Data_2008_20_Bihar$fertiliser_p_rs_kg=CACP_Data_2008_20_Bihar$fertiliser_p_rs/CACP_Data_2008_20_Bihar$fertiliser_p_kgCACP_Data_2008_20_Bihar$fertiliser_k_rs_kg=CACP_Data_2008_20_Bihar$fertiliser_k_rs/CACP_Data_2008_20_Bihar$fertiliser_k_kg#IrrigationCACP_Data_2008_20_Bihar$own_irrigation_machine_rs_hrs=CACP_Data_2008_20_Bihar$own_irrigation_machine_rs/CACP_Data_2008_20_Bihar$own_irrigation_machine_hrsCACP_Data_2008_20_Bihar$hired_irrigation_machine_rs_hrs=CACP_Data_2008_20_Bihar$hired_irrigation_machine_rs/CACP_Data_2008_20_Bihar$hired_irrigation_machine_hrs#LaborCACP_Data_2008_20_Bihar$family_labour_rs_hrs=CACP_Data_2008_20_Bihar$family_labour_rs/CACP_Data_2008_20_Bihar$family_labour_hrsCACP_Data_2008_20_Bihar$family_labour_rs_ha=CACP_Data_2008_20_Bihar$family_labour_rs/CACP_Data_2008_20_Bihar$crop_area_haCACP_Data_2008_20_Bihar$family_labour_hrs=CACP_Data_2008_20_Bihar$family_labour_hrs/CACP_Data_2008_20_Bihar$crop_area_haCACP_Data_2008_20_Bihar$casual_labour_rs_hrs=CACP_Data_2008_20_Bihar$casual_labour_rs/CACP_Data_2008_20_Bihar$casual_labour_hrsCACP_Data_2008_20_Bihar$casual_labour_rs_ha=CACP_Data_2008_20_Bihar$casual_labour_rs/CACP_Data_2008_20_Bihar$crop_area_haCACP_Data_2008_20_Bihar$casual_labour_hrs_ha=CACP_Data_2008_20_Bihar$casual_labour_hrs/CACP_Data_2008_20_Bihar$crop_area_haCACP_Data_2008_20_Bihar$main_product_rs_kg=CACP_Data_2008_20_Bihar$main_product_rs/(CACP_Data_2008_20_Bihar$main_product_qtls*100)CACP_Data_2008_20_Bihar_Inputs=subset(CACP_Data_2008_20_Bihar,select=c("fertiliser_n_rs_kg","fertiliser_p_rs_kg","fertiliser_k_rs_kg","own_irrigation_machine_rs_hrs","hired_irrigation_machine_rs_hrs","family_labour_rs_hrs","family_labour_rs_ha","casual_labour_rs_hrs","casual_labour_rs_ha","main_product_rs_kg","district_name","year"))CACP_Data_2008_20_Bihar_Inputs$year=as.factor(CACP_Data_2008_20_Bihar_Inputs$year) CACP_Data_2008_20_Bihar_Inputs_dist=CACP_Data_2008_20_Bihar_Inputs[,lapply(.SD,median,na.rm=TRUE),by=.(district_name,year)]library(dplyr)library(tidyr)# Replace all NAs with the column medianCACP_Data_2008_20_Bihar_Inputs_dist =CACP_Data_2008_20_Bihar_Inputs_dist %>%mutate(across(where(is.numeric), ~replace_na(., median(., na.rm=TRUE))))# CACP_Data_2008_20_Bihar_Inputs_dist$district_name[CACP_Data_2008_20_Bihar_Inputs_dist$district_name=="East Champaran"]="EastChamparan"# Summarize the CSISA_KVK data ### N -----------------------------------CSISA_KVK$BasalDAPN=CSISA_KVK$BasalDAP*0.18table(CSISA_KVK$GradeNPK)
CSISA_KVK$GradeNPKN=NACSISA_KVK$GradeNPKN[CSISA_KVK$GradeNPK=="10.26.26"]=0.1CSISA_KVK$GradeNPKN[CSISA_KVK$GradeNPK=="12.32.16"]=0.12CSISA_KVK$GradeNPKN[CSISA_KVK$GradeNPK=="20.20.0.13"]=0.2CSISA_KVK$GradeNPKP=NACSISA_KVK$GradeNPKP[CSISA_KVK$GradeNPK=="10.26.26"]=0.26CSISA_KVK$GradeNPKP[CSISA_KVK$GradeNPK=="12.32.16"]=0.32CSISA_KVK$GradeNPKP[CSISA_KVK$GradeNPK=="20.20.0.13"]=0.2CSISA_KVK$GradeNPKK=NACSISA_KVK$GradeNPKK[CSISA_KVK$GradeNPK=="10.26.26"]=0.26CSISA_KVK$GradeNPKK[CSISA_KVK$GradeNPK=="12.32.16"]=0.16CSISA_KVK$GradeNPKK[CSISA_KVK$GradeNPK=="20.20.0.13"]=0CSISA_KVK$BasalNPKN=CSISA_KVK$BasalNPK*CSISA_KVK$GradeNPKNCSISA_KVK$Split1UreaN=CSISA_KVK$Split1Urea*0.46CSISA_KVK$Split2UreaN=CSISA_KVK$Split2Urea*0.46CSISA_KVK$Split3UreaN=CSISA_KVK$Split3Urea*0.46# NCSISA_KVK$N=rowSums(CSISA_KVK[,c("BasalDAPN","BasalNPKN","Split1UreaN","Split2UreaN","Split3UreaN")],na.rm =TRUE)### P ------------------------------------CSISA_KVK$BasalDAPP=CSISA_KVK$BasalDAP*0.46CSISA_KVK$BasalNPKP=CSISA_KVK$BasalNPK*CSISA_KVK$GradeNPKPCSISA_KVK$P=rowSums(CSISA_KVK[,c("BasalDAPP","BasalNPKP")],na.rm =TRUE)### KCSISA_KVK$BasalMOPK=CSISA_KVK$BasalMOP*0.6CSISA_KVK$BasalNPKK=CSISA_KVK$BasalNPK*CSISA_KVK$GradeNPKKCSISA_KVK$K=rowSums(CSISA_KVK[,c("BasalMOPK","BasalNPKK")],na.rm =TRUE)# Merge with price data by district and year CSISA_KVK_CACP=merge(CSISA_KVK,CACP_Data_2008_20_Bihar_Inputs_dist,by.x=c("District","Year"),by.y=c("district_name","year"),all.x=TRUE)table(CSISA_KVK_CACP$District)
Ara Begusarai Buxar East Champaran Lakhisarai
325 247 402 585 288
Madhepura Muzaffarpur Rohtas
442 281 293
table(CSISA_KVK$District)
Ara Begusarai Buxar East Champaran Lakhisarai
325 247 402 585 288
Madhepura Muzaffarpur Rohtas
442 281 293
Min. 1st Qu. Median Mean 3rd Qu. Max.
23630 52320 65120 65644 77280 113220
# N fert costsCSISA_KVK_CACP$N_cost_rs_ha=CSISA_KVK_CACP$N*CSISA_KVK_CACP$fertiliser_n_rs_kgCSISA_KVK_CACP$P_cost_rs_ha=CSISA_KVK_CACP$P*CSISA_KVK_CACP$fertiliser_p_rs_kgCSISA_KVK_CACP$K_cost_rs_ha=CSISA_KVK_CACP$K*CSISA_KVK_CACP$fertiliser_k_rs_kgCSISA_KVK_CACP$Hired_irrig_cost_rs_ha=CSISA_KVK_CACP$IrrigationNumber*10*CSISA_KVK_CACP$hired_irrigation_machine_rs_hrs# Total variable costs CSISA_KVK_CACP$Wheat_total_costs_rs_ha=rowSums(CSISA_KVK_CACP[,c("N_cost_rs_ha","P_cost_rs_ha","K_cost_rs_ha","Hired_irrig_cost_rs_ha","casual_labour_rs_ha")],na.rm =TRUE)# Partial profits CSISA_KVK_CACP$partial_profit_rs_ha=CSISA_KVK_CACP$Wheat_revenue_rs_ha-CSISA_KVK_CACP$Wheat_total_costs_rs_ha# Plot total costs by variety and sowing dates # Plot profitslibrary(ggpubr)ggerrorplot(CSISA_KVK_CACP, x ="Variety", y ="partial_profit_rs_ha",add ="mean", ggtheme=theme_bw(), color ="SowingSchedule", palette ="Paired",error.plot ="pointrange",position =position_dodge(0.5))+labs(x="Variety",y="Partial profit (Rs/ha)")
ggsave("figures/profit_differences_major_varieties.png")# Get district level estimateslibrary(data.table) CSISA_KVK_CACP=data.table(CSISA_KVK_CACP)CSISA_KVK_CACP_dist=CSISA_KVK_CACP[,lapply(.SD,base::mean,na.rm=TRUE),by=.(District,Year,Variety_SowingSchedule)]CSISA_KVK_CACP_state=CSISA_KVK_CACP[,lapply(.SD,base::mean,na.rm=TRUE),by=.(Year,Variety_SowingSchedule)]
5.1 Yield
CSISA_KVK_CACP_state_wide=xtabs(GrainYield ~ Year + Variety_SowingSchedule, data = CSISA_KVK_CACP_state)class(CSISA_KVK_CACP_state_wide)=NULLCSISA_KVK_CACP_state_wide=as.data.frame(CSISA_KVK_CACP_state_wide)# Remove the columns with NACSISA_KVK_CACP_state_wide=na_if(CSISA_KVK_CACP_state_wide, 0)CSISA_KVK_CACP_state_wide=CSISA_KVK_CACP_state_wide %>%select_if(~!any(is.na(.)))library(lubridate)date=dmy(c("01-01-2016","01-01-2017","01-01-2018","01-01-2019","01-01-2020"))CSISA_KVK_CACP_state_wide$date=datelibrary(fPortfolio)library(timeSeries)rownames(CSISA_KVK_CACP_state_wide) <- CSISA_KVK_CACP_state_wide$dateCSISA_KVK_CACP_state_wide$date=NULLCSISA_KVK_CACP_state_wide_ts <-timeSeries(CSISA_KVK_CACP_state_wide)covOGKEstimate <-covOGKEstimator(CSISA_KVK_CACP_state_wide_ts)fastCovOGKEstimator <-function(x, spec =NULL, ...) covOGKEstimatecovOGKSpec <-portfolioSpec()setEstimator(covOGKSpec) <-"fastCovOGKEstimator"setNFrontierPoints(covOGKSpec) <-5covOGKFrontier <-portfolioFrontier(data = CSISA_KVK_CACP_state_wide_ts, spec = covOGKSpec)print(covOGKFrontier)
png("figures/State_variety_sowingschedule_weights.png")col =divPalette(15, "RdYlGn")weightsPlot(covOGKFrontier, col = col, mtext =FALSE)text <-"OGK Robustified MV Portfolio"mtext(text, side =3, line =3, font =2, cex =0.9)dev.off()
png("figures/Revenue_State_variety_sowingschedule_weights.png")col =divPalette(15, "RdYlGn")weightsPlot(covOGKFrontier, col = col, mtext =FALSE)text <-"OGK Robustified MV Portfolio"mtext(text, side =3, line =3, font =2, cex =0.9)dev.off()
col <-divPalette(15, "RdYlGn")weightsPlot(covOGKFrontier, col = col, mtext =FALSE)text <-"OGK Robustified MV Portfolio"mtext(text, side =3, line =3, font =2, cex =0.9)
png("figures/Profit_State_variety_sowingschedule_weights.png")col =divPalette(15, "RdYlGn")weightsPlot(covOGKFrontier, col = col, mtext =FALSE)text <-"OGK Robustified MV Portfolio"mtext(text, side =3, line =3, font =2, cex =0.9)dev.off()