# Xiaofeng Xu created this file in May 2020, and updated in Sep 2020, and finalized with 14C on Nov 11 2020
library("RNetCDF")
library("akima")
rm(list=ls())
setwd("/Users/xxuadmin/BUSINESS/PUBLICATIONS/WorkingOn_Xu_Isotope/SPRUCE_2020Sep")
# 13C is based on the PD standard Pee Dee Belemnite (PDB). 13C:12C = 0.0112372.
# 13C/12C(sample) / 13C/12C(standard - 1)
# 13C = (delta / 1000 + 1) * 0.0112372 * 12C

# for the spruce site, the 14C = 12C * ((1 + delta14C / 1000) * (1.176e-12/((1+delta13C)*(1+delta13C)/(1-0.025)/(1-0.025)))
# the 13Cdealta is -25 per mil for standard sample; this is for SPRUCE site based on the data from Erik Hobbie at UNH

data <- read.csv("Peat_Characteristics_T0_20180425_plot.csv", header=TRUE)
O13C <- data[1:16,3:4]
O14C <- data[1:16,5]
#depth 25, 15, 5, -5, -25, -35, -45, -55, -65, -85, -113, -163, -188, -225, -255
#CLM depth 0.71, 2.8, 6.2, 11.9, 21.2, 36.6, 61.97, 103.8, 172.8, 286.5, 473.9, 782.97, 1292.5, 2132.6, 3517.8

depth <- c(0.0071, 0.0028, 0.0623, 0.1189, 0.2122, 0.366, 0.6197, 1.038, 1.72, 2.86)

#Hommock
#25 for layer 1, 2, 3
#15 for layer 4
# 5 for layer 5,
#-5 for layer 6,
#-25, -35, -45 for layer 7
#-55, -65, and -85 for layer 8
# -113, -163, and -188 for layer 9
# -255 for layer 10 2.86

#Hollow
#-5 for layer 1, 2, 3
# -15 for layer 4,
#-25 for layer 5,
#-35 for layer 6
#-45 -55, and -65 for layer 7 0.6197
#-85 and -133 for layer 8 1.038
# -163 and -188 for layer 9 1.72
# -225 and -275 for layer 10 2.86
SO13C = array(0, dim=c(15,2))
#L13C = O13C[3,1]
SO13C[1,1] = O13C[1,1]
SO13C[2,1] = O13C[1,1]
SO13C[3,1] = O13C[1,1]
SO13C[4,1] = O13C[2,1]
SO13C[5,1] = O13C[3,1]
SO13C[6,1] = O13C[4,1]
SO13C[7,1] = mean(O13C[5:7,1])
SO13C[8,1] = mean(O13C[8:10,1])
SO13C[9,1] = mean(O13C[11:13,1])
SO13C[10,1] = mean(O13C[14:15,1])

SO13C[1,2] = mean(O13C[3:4,1])
SO13C[2,2] = mean(O13C[3:4,1])
SO13C[3,2] = mean(O13C[3:4,1])
SO13C[4,2] = mean(O13C[4:5,1])
SO13C[5,2] = mean(O13C[6:7,1])
SO13C[6,2] = mean(O13C[8:9,1])
SO13C[7,2] = mean(O13C[8:9,1])
SO13C[8,2] = mean(O13C[10:11,1])
SO13C[9,2] = mean(O13C[12:13,1])
SO13C[10,2] = mean(O13C[14:16,1])

#plot(SO13C[1:10,1],-depth,type="l",xlab="soil 13CH4 (mmol/L)", ylab="soil depth (m)",cex=1,las=1, cex.lab = 1.5, cex.axis = 1.5,lwd=2)

SO14C = array(0, dim=c(15,2))
SO14C[1,1] = O14C[1]
SO14C[2,1] = O14C[1]
SO14C[3,1] = O14C[1]
SO14C[4,1] = O14C[2]
SO14C[5,1] = O14C[3]
SO14C[6,1] = O14C[4]
SO14C[7,1] = mean(O14C[5:7])
SO14C[8,1] = mean(O14C[8:10])
SO14C[9,1] = mean(O14C[11:13])
SO14C[10,1] = mean(O14C[14:15])

SO14C[1,2] = O14C[4]
SO14C[2,2] = O14C[4]
SO14C[3,2] = O14C[4]
SO14C[4,2] = O14C[5]
SO14C[5,2] = O14C[6]
SO14C[6,2] = mean(O14C[7:9])
SO14C[7,2] = mean(O14C[7:9])
SO14C[8,2] = mean(O14C[10:11])
SO14C[9,2] = mean(O14C[12:13])
SO14C[10,2] = mean(O14C[14:16])

#13C_ave	Depth_ave
#-29.058	25
#-28.34	15
#-28.1075	5
#-28.77631579	-5
#-27.79684211	-15
#-27.006	-25
#-26.72888889	-35
#-26.45055556	-45
#-26.60555556	-55
#-26.36611111	-65
#-26.05333333	-85
#-25.93	-113
#-26.39176471	-163
#-26.27	-188
#-26.63857143	-225
#-25.75	-275

#depth	14C_mean	14Cto12C
#25	40.65714286	1.23406E-12
#15	54.37692308	1.24849E-12
#5	112.4714286	1.31665E-12
#-5	68.17647059	1.26596E-12
#-15	140.0176471	1.34839E-12
#-25	114.2	        1.31571E-12
#-35	-99.17647059	1.06314E-12
#-45	-180.2058824	9.66953E-13
#-55	-199.9941176	9.43913E-13
#-65	-286.2058824	8.41779E-13
#-85	-358.8117647	7.55669E-13
#-113	-392.4058824	7.15896E-13
#-163	-486.13125	6.06039E-13
#-188	-350.8	        7.65452E-13
#-225	-571.1	        5.06086E-13
#-275	-703.75	        3.48927E-13

computC13 <- function(C12, deltaC)
{
        (deltaC / 1000.0 + 1.0) * 0.0112372 * C12
}

computC14 <- function(C12, deltaC13, deltaC14)
{
        C12 * ((1.0 + deltaC14 / 1000) * (1.176e-12/((1+deltaC13)*(1+deltaC13)/(1-0.000025)/(1-0.000025))))
}

#14C = 12C * ((1 + delta14C / 1000) * (1.176e-12/((1+delta13C)*(1+delta13C)/(1-0.025)/(1-0.025)))

#notice: please copy a file with the name of fileoutput in the following code for variable to be incorporated
fileinput <- open.nc("SPRUCE-finalspinup-peatland-carbon-initial.nc")  # 2012 year climate data
#filerestart <- open.nc("SPR_I20TRCLM45CN_adspinup.clm2.r.1201-01-01-00000.nc")
fileoutput <- open.nc("SPRUCE-finalspinup-peatland-ISOcarbon-initial.nc",write=TRUE)

#depth <- c(0.0071, 0.0028, 0.0623, 0.1189, 0.2122, 0.366, 0.6197, 1.038, 1.72, 2.86)
#thick <- c(0.0175128179, 0.0275789693, 0.0454700332, 0.0749674110, 0.1236003651, 0.2037825510, 0.3359806264, 0.5539384054, 0.9132900316, 1.5057607014)
#realcarbondensity = c(22385, 22385, 22385, 30294, 43978, 95584, 92983, 83658, 83799, 90616)

cwdc_vr <- var.get.nc(fileinput, "cwdc_vr")
litr1c_vr <- var.get.nc(fileinput, "litr1c_vr")
litr2c_vr <- var.get.nc(fileinput, "litr2c_vr")
litr3c_vr <- var.get.nc(fileinput, "litr3c_vr")
soil1c_vr <- var.get.nc(fileinput, "soil1c_vr")
soil2c_vr <- var.get.nc(fileinput, "soil2c_vr")
soil3c_vr <- var.get.nc(fileinput, "soil3c_vr")
soil4c_vr <- var.get.nc(fileinput, "soil4c_vr")

cwdn_vr <- var.get.nc(fileinput, "cwdn_vr")
litr1n_vr <- var.get.nc(fileinput, "litr1c_vr")
litr2n_vr <- var.get.nc(fileinput, "litr2n_vr")
litr3n_vr <- var.get.nc(fileinput, "litr3n_vr")
soil1n_vr <- var.get.nc(fileinput, "soil1n_vr")
soil2n_vr <- var.get.nc(fileinput, "soil2n_vr")
soil3n_vr <- var.get.nc(fileinput, "soil3n_vr")
soil4n_vr <- var.get.nc(fileinput, "soil4n_vr")

#rc13_canair <- var.get.nc(filerestart, "rc13_canair")
#rc13_psnsun <- var.get.nc(filerestart, "rc13_psnsun")
#rc13_psnsha <- var.get.nc(filerestart, "rc13_psnsha")
#leafc_13 <- var.get.nc(filerestart, "leafc_13")
#leafc_storage_13 <- var.get.nc(filerestart, "leafc_storage_13")
#leafc_xfer_13 <- var.get.nc(filerestart, "leafc_xfer_13")
#frootc_13 <- var.get.nc(filerestart, "frootc_13")
#frootc_storage_13 <- var.get.nc(filerestart, "frootc_storage_13")
#frootc_xfer_13 <- var.get.nc(filerestart, "frootc_xfer_13")
#livestemc_13 <- var.get.nc(filerestart, "livestemc_13")
#livestemc_storage_13 <- var.get.nc(filerestart, "livestemc_storage_13")
#livestemc_xfer_13 <- var.get.nc(filerestart, "livestemc_xfer_13")
#deadstemc_13 <- var.get.nc(filerestart, "deadstemc_13")
#deadstemc_storage_13 <- var.get.nc(filerestart, "deadstemc_storage_13")
#deadstemc_xfer_13 <- var.get.nc(filerestart, "deadstemc_xfer_13")
#livecrootc_13 <- var.get.nc(filerestart, "livecrootc_13")
#deadstemc_13 <- var.get.nc(filerestart, "deadstemc_13")

cwdc_13_vr = cwdc_vr
litr1c_13_vr = litr1c_vr
litr2c_13_vr = litr2c_vr
litr3c_13_vr = litr3c_vr
soil1c_13_vr = soil1c_vr
soil2c_13_vr = soil2c_vr
soil3c_13_vr = soil3c_vr
soil4c_13_vr = soil4c_vr

# C14
cwdc_14_vr = cwdc_vr
litr1c_14_vr = litr1c_vr
litr2c_14_vr = litr2c_vr
litr3c_14_vr = litr3c_vr
soil1c_14_vr = soil1c_vr
soil2c_14_vr = soil2c_vr
soil3c_14_vr = soil3c_vr
soil4c_14_vr = soil4c_vr

bacteriac_vr = litr1c_vr
fungic_vr = litr1c_vr
domc_vr = litr1c_vr
seedc = litr1c_vr
col_ctrunc_vr = litr1c_vr

totlitc = litr1c_vr
totcolc = litr1c_vr
prod10c = litr1c_vr
prod100c = litr1c_vr

bacteriac_13_vr = bacteriac_vr
fungic_13_vr = fungic_vr
domc_13_vr = domc_vr
seedc_13 = seedc
col_ctrunc_13_vr = col_ctrunc_vr

totlitc_13 = totlitc
totcolc_13 = totcolc
prod10c_13 = prod10c
prod100c_13 = prod100c

#C14
bacteriac_14_vr = bacteriac_vr
fungic_14_vr = fungic_vr
domc_14_vr = domc_vr
seedc_14 = seedc
col_ctrunc_14_vr = col_ctrunc_vr

totlitc_14 = totlitc
totcolc_14 = totcolc
prod10c_14 = prod10c
prod100c_14 = prod100c

for(i in 1:10)
{
cwdc_13_vr[i,1] = computC13(cwdc_vr[i,1], SO13C[i])
litr1c_13_vr[i,1] = computC13(litr1c_vr[i,1], SO13C[i])
litr2c_13_vr[i,1] = computC13(litr2c_vr[i,1], SO13C[i])
litr3c_13_vr[i,1] = computC13(litr3c_vr[i,1], SO13C[i])
soil1c_13_vr[i,1] = computC13(soil1c_vr[i,1], SO13C[i])
soil2c_13_vr[i,1] = computC13(soil2c_vr[i,1], SO13C[i])
soil3c_13_vr[i,1] = computC13(soil3c_vr[i,1], SO13C[i])
soil4c_13_vr[i,1] = computC13(soil4c_vr[i,1], SO13C[i])

bacteriac_13_vr[i,1] = computC13(bacteriac_vr[i,1], SO13C[i])
fungic_13_vr[i,1] = computC13(fungic_vr[i,1], SO13C[i])
domc_13_vr[i,1] = computC13(domc_vr[i,1], SO13C[i])
seedc_13[i,1] = computC13(seedc[i,1], SO13C[i])
col_ctrunc_13_vr[i,1] = computC13(col_ctrunc_vr[i,1], SO13C[i])
totlitc_13[i,1] = computC13(totlitc[i,1], SO13C[i])
totcolc_13[i,1] = computC13(totcolc[i,1], SO13C[i])
prod10c_13[i,1] = computC13(prod10c[i,1], SO13C[i])
prod100c_13[i,1] = computC13(prod100c[i,1], SO13C[i])

cwdc_13_vr[i,2] = computC13(cwdc_vr[i,2], SO13C[i])
litr1c_13_vr[i,2] = computC13(litr1c_vr[i,2], SO13C[i])
litr2c_13_vr[i,2] = computC13(litr2c_vr[i,2], SO13C[i])
litr3c_13_vr[i,2] = computC13(litr3c_vr[i,2], SO13C[i])
soil1c_13_vr[i,2] = computC13(soil1c_vr[i,2], SO13C[i])
soil2c_13_vr[i,2] = computC13(soil2c_vr[i,2], SO13C[i])
soil3c_13_vr[i,2] = computC13(soil3c_vr[i,2], SO13C[i])
soil4c_13_vr[i,2] = computC13(soil4c_vr[i,2], SO13C[i])

bacteriac_13_vr[i,2] = computC13(bacteriac_vr[i,2], SO13C[i])
fungic_13_vr[i,2] = computC13(fungic_vr[i,2], SO13C[i])
domc_13_vr[i,2] = computC13(domc_vr[i,2], SO13C[i])
seedc_13[i,2] = computC13(seedc[i,2], SO13C[i])
col_ctrunc_13_vr[i,2] = computC13(col_ctrunc_vr[i,2], SO13C[i])
totlitc_13[i,2] = computC13(totlitc[i,2], SO13C[i])
totcolc_13[i,2] = computC13(totcolc[i,2], SO13C[i])
prod10c_13[i,2] = computC13(prod10c[i,2], SO13C[i])
prod100c_13[i,2] = computC13(prod100c[i,2], SO13C[i])
}

for(i in 11:15)
{
cwdc_13_vr[i,1] = 0.0
litr1c_13_vr[i,1] = 0.0
litr2c_13_vr[i,1] = 0.0
litr3c_13_vr[i,1] = 0.0
soil1c_13_vr[i,1] = 0.0
soil2c_13_vr[i,1] = 0.0
soil3c_13_vr[i,1] = 0.0
soil4c_13_vr[i,1] = 0.0

bacteriac_13_vr[i,1] = 0.0
fungic_13_vr[i,1] = 0.0
domc_13_vr[i,1] = 0.0
seedc_13[i,1] = 0.0
col_ctrunc_13_vr[i,1] = 0.0

totlitc_13[i,1] = 0.0
totcolc_13[i,1] = 0.0
prod10c_13[i,1] = 0.0
prod100c_13[i,1] = 0.0

cwdc_13_vr[i,2] = 0.0
litr1c_13_vr[i,2] = 0.0
litr2c_13_vr[i,2] = 0.0
litr3c_13_vr[i,2] = 0.0
soil1c_13_vr[i,2] = 0.0
soil2c_13_vr[i,2] = 0.0
soil3c_13_vr[i,2] = 0.0
soil4c_13_vr[i,2] = 0.0

bacteriac_13_vr[i,2] = 0.0
fungic_13_vr[i,2] = 0.0
domc_13_vr[i,2] = 0.0
seedc_13[i,2] = 0.0
col_ctrunc_13_vr[i,2] = 0.0

totlitc_13[i,2] = 0.0
totcolc_13[i,2] = 0.0
prod10c_13[i,2] = 0.0
prod100c_13[i,2] = 0.0
}

for(i in 1:10)
{
        cwdc_14_vr[i,1] = computC14(cwdc_vr[i,1], cwdc_13_vr[i,1], SO14C[i])
        litr1c_14_vr[i,1] = computC14(litr1c_vr[i,1], litr1c_13_vr[i,1], SO14C[i])
        litr2c_14_vr[i,1] = computC14(litr2c_vr[i,1], litr2c_13_vr[i,1], SO14C[i])
        litr3c_14_vr[i,1] = computC14(litr3c_vr[i,1], litr3c_13_vr[i,1], SO14C[i])
        soil1c_14_vr[i,1] = computC14(soil1c_vr[i,1], soil1c_13_vr[i,1], SO14C[i])
        soil2c_14_vr[i,1] = computC14(soil2c_vr[i,1], soil2c_13_vr[i,1], SO14C[i])
        soil3c_14_vr[i,1] = computC14(soil3c_vr[i,1], soil3c_13_vr[i,1], SO14C[i])
        soil4c_14_vr[i,1] = computC14(soil4c_vr[i,1], soil4c_13_vr[i,1], SO14C[i])

        bacteriac_14_vr[i,1] = computC14(bacteriac_vr[i,1], bacteriac_13_vr[i,1], SO14C[i])
        fungic_14_vr[i,1] = computC14(fungic_vr[i,1], fungic_13_vr[i,1], SO14C[i])
        domc_14_vr[i,1] = computC14(domc_vr[i,1], domc_13_vr[i,1], SO14C[i])
        seedc_14[i,1] = computC14(seedc[i,1], seedc_13[i,1], SO14C[i])
        col_ctrunc_14_vr[i,1] = computC14(col_ctrunc_vr[i,1], col_ctrunc_13_vr[i,1], SO14C[i])
        totlitc_14[i,1] = computC14(totlitc[i,1], totlitc_13[i,1], SO14C[i])
        totcolc_14[i,1] = computC14(totcolc[i,1], totcolc_13[i,1], SO14C[i])
        prod10c_14[i,1] = computC14(prod10c[i,1], prod10c_13[i,1], SO14C[i])
        prod100c_14[i,1] = computC14(prod100c[i,1], prod100c_13[i,1], SO14C[i])

        cwdc_14_vr[i,2] = computC14(cwdc_vr[i,2], cwdc_13_vr[i,2], SO14C[i])
        litr1c_14_vr[i,2] = computC14(litr1c_vr[i,2], litr1c_13_vr[i,2], SO14C[i])
        litr2c_14_vr[i,2] = computC14(litr2c_vr[i,2], litr2c_13_vr[i,2], SO14C[i])
        litr3c_14_vr[i,2] = computC14(litr3c_vr[i,2], litr3c_13_vr[i,2], SO14C[i])
        soil1c_14_vr[i,2] = computC14(soil1c_vr[i,2], soil1c_13_vr[i,2], SO14C[i])
        soil2c_14_vr[i,2] = computC14(soil2c_vr[i,2], soil2c_13_vr[i,2], SO14C[i])
        soil3c_14_vr[i,2] = computC14(soil3c_vr[i,2], soil3c_13_vr[i,2], SO14C[i])
        soil4c_14_vr[i,2] = computC14(soil4c_vr[i,2], soil4c_13_vr[i,2], SO14C[i])

        bacteriac_14_vr[i,2] = computC14(bacteriac_vr[i,2], bacteriac_13_vr[i,2], SO14C[i])
        fungic_14_vr[i,2] = computC14(fungic_vr[i,2], fungic_13_vr[i,2], SO14C[i])
        domc_14_vr[i,2] = computC14(domc_vr[i,2], domc_13_vr[i,2], SO14C[i])
        seedc_14[i,2] = computC14(seedc[i,2], seedc_13[i,2], SO14C[i])
        col_ctrunc_14_vr[i,2] = computC14(col_ctrunc_vr[i,2], col_ctrunc_13_vr[i,2], SO14C[i])
        totlitc_14[i,2] = computC14(totlitc[i,2], totlitc_13[i,2], SO14C[i])
        totcolc_14[i,2] = computC14(totcolc[i,2], totcolc_13[i,2], SO14C[i])
        prod10c_14[i,2] = computC14(prod10c[i,2], prod10c_13[i,2], SO14C[i])
        prod100c_14[i,2] = computC14(prod100c[i,2], prod100c_13[i,2], SO14C[i])
}

for(i in 11:15)
{
        cwdc_14_vr[i,1] = 0.0
        litr1c_14_vr[i,1] = 0.0
        litr2c_14_vr[i,1] = 0.0
        litr3c_14_vr[i,1] = 0.0
        soil1c_14_vr[i,1] = 0.0
        soil2c_14_vr[i,1] = 0.0
        soil3c_14_vr[i,1] = 0.0
        soil4c_14_vr[i,1] = 0.0

        bacteriac_14_vr[i,1] = 0.0
        fungic_14_vr[i,1] = 0.0
        domc_14_vr[i,1] = 0.0
        seedc_14[i,1] = 0.0
        col_ctrunc_14_vr[i,1] = 0.0

        totlitc_14[i,1] = 0.0
        totcolc_14[i,1] = 0.0
        prod10c_14[i,1] = 0.0
        prod100c_14[i,1] = 0.0

        cwdc_14_vr[i,2] = 0.0
        litr1c_14_vr[i,2] = 0.0
        litr2c_14_vr[i,2] = 0.0
        litr3c_14_vr[i,2] = 0.0
        soil1c_14_vr[i,2] = 0.0
        soil2c_14_vr[i,2] = 0.0
        soil3c_14_vr[i,2] = 0.0
        soil4c_14_vr[i,2] = 0.0

        bacteriac_14_vr[i,2] = 0.0
        fungic_14_vr[i,2] = 0.0
        domc_14_vr[i,2] = 0.0
        seedc_14[i,2] = 0.0
        col_ctrunc_14_vr[i,2] = 0.0

        totlitc_14[i,2] = 0.0
        totcolc_14[i,2] = 0.0
        prod10c_14[i,2] = 0.0
        prod100c_14[i,2] = 0.0
}

#
#rc13_canair = c(0.005582864, 0.002791432)
#rc13_psnsha = c(0.005544063, 0.002771556)
#rc13_psnsun = c(0.0055442, 0.002771556)
#

#levgrnd = 15
#column = 2
#var.def.nc(fileoutput,"levgrnd","NC_FLOAT","levgrnd")
#var.def.nc(fileoutput,"column","NC_FLOAT","column")

var.def.nc(fileoutput,"cwdc_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"litr1c_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"litr2c_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"litr3c_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil1c_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil2c_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil3c_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil4c_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"bacteriac_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"fungic_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"domc_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"seedc_13","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"col_ctrunc_13_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"totlitc_13","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"totcolc_13","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"prod10c_13","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"prod100c_13","NC_FLOAT",c("levgrnd","column"))

var.def.nc(fileoutput,"cwdc_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"litr1c_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"litr2c_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"litr3c_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil1c_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil2c_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil3c_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"soil4c_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"bacteriac_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"fungic_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"domc_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"seedc_14","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"col_ctrunc_14_vr","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"totlitc_14","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"totcolc_14","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"prod10c_14","NC_FLOAT",c("levgrnd","column"))
var.def.nc(fileoutput,"prod100c_14","NC_FLOAT",c("levgrnd","column"))

var.put.nc(fileoutput, "cwdc_vr",cwdc_vr)
var.put.nc(fileoutput, "litr1c_vr",litr1c_vr)
var.put.nc(fileoutput, "litr2c_vr",litr2c_vr)
var.put.nc(fileoutput, "litr3c_vr",litr3c_vr)
var.put.nc(fileoutput, "soil1c_vr",soil1c_vr)
var.put.nc(fileoutput, "soil2c_vr",soil2c_vr)
var.put.nc(fileoutput, "soil3c_vr",soil3c_vr)
var.put.nc(fileoutput, "soil4c_vr",soil4c_vr)

var.put.nc(fileoutput, "cwdn_vr",cwdn_vr)
var.put.nc(fileoutput, "litr1n_vr",litr1n_vr)
var.put.nc(fileoutput, "litr2n_vr",litr2n_vr)
var.put.nc(fileoutput, "litr3n_vr",litr3n_vr)
var.put.nc(fileoutput, "soil1n_vr",soil1n_vr)
var.put.nc(fileoutput, "soil2n_vr",soil2n_vr)
var.put.nc(fileoutput, "soil3n_vr",soil3n_vr)
var.put.nc(fileoutput, "soil4n_vr",soil4n_vr)

att.put.nc(fileoutput, "cwdc_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "cwdc_13_vr",cwdc_13_vr)

att.put.nc(fileoutput, "litr1c_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "litr1c_13_vr",litr1c_13_vr)

att.put.nc(fileoutput, "litr2c_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "litr2c_13_vr",litr2c_13_vr)

att.put.nc(fileoutput, "litr3c_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "litr3c_13_vr",litr3c_13_vr)

att.put.nc(fileoutput, "soil1c_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil1c_13_vr",soil1c_13_vr)

att.put.nc(fileoutput, "soil2c_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil2c_13_vr",soil2c_13_vr)

att.put.nc(fileoutput, "soil3c_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil3c_13_vr",soil3c_13_vr)

att.put.nc(fileoutput, "soil4c_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil4c_13_vr",soil4c_13_vr)

att.put.nc(fileoutput, "bacteriac_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "bacteriac_13_vr",bacteriac_13_vr)

att.put.nc(fileoutput, "fungic_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "fungic_13_vr",fungic_13_vr)

att.put.nc(fileoutput, "domc_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "domc_13_vr",domc_13_vr)

att.put.nc(fileoutput, "seedc_13","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "seedc_13",seedc_13)

att.put.nc(fileoutput, "col_ctrunc_13_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "col_ctrunc_13_vr",col_ctrunc_13_vr)

att.put.nc(fileoutput, "totlitc_13","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "totlitc_13",totlitc_13)

att.put.nc(fileoutput, "totcolc_13","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "totcolc_13",totcolc_13)

att.put.nc(fileoutput, "prod10c_13","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "prod10c_13",prod10c_13)

att.put.nc(fileoutput, "prod100c_13","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "prod100c_13",prod100c_13)

# 14C
att.put.nc(fileoutput, "cwdc_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "cwdc_14_vr",cwdc_14_vr)

att.put.nc(fileoutput, "litr1c_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "litr1c_14_vr",litr1c_14_vr)

att.put.nc(fileoutput, "litr2c_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "litr2c_14_vr",litr2c_14_vr)

att.put.nc(fileoutput, "litr3c_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "litr3c_14_vr",litr3c_14_vr)

att.put.nc(fileoutput, "soil1c_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil1c_14_vr",soil1c_14_vr)

att.put.nc(fileoutput, "soil2c_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil2c_14_vr",soil2c_14_vr)

att.put.nc(fileoutput, "soil3c_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil3c_14_vr",soil3c_14_vr)

att.put.nc(fileoutput, "soil4c_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "soil4c_14_vr",soil4c_14_vr)

att.put.nc(fileoutput, "bacteriac_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "bacteriac_14_vr",bacteriac_14_vr)

att.put.nc(fileoutput, "fungic_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "fungic_14_vr",fungic_14_vr)

att.put.nc(fileoutput, "domc_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "domc_14_vr",domc_14_vr)

att.put.nc(fileoutput, "seedc_14","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "seedc_14",seedc_14)

att.put.nc(fileoutput, "col_ctrunc_14_vr","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "col_ctrunc_14_vr",col_ctrunc_14_vr)

att.put.nc(fileoutput, "totlitc_14","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "totlitc_14",totlitc_14)

att.put.nc(fileoutput, "totcolc_14","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "totcolc_14",totcolc_14)

att.put.nc(fileoutput, "prod10c_14","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "prod10c_14",prod10c_14)

att.put.nc(fileoutput, "prod100c_14","missing_value", "NC_FLOAT", -9999)
var.put.nc(fileoutput, "prod100c_14",prod100c_14)

# new file
#var.def.nc(fileoutput,"rc13_canair","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "rc13_canair","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "rc13_canair",rc13_canair)

#var.def.nc(fileoutput,"rc13_psnsun","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "rc13_psnsun","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "rc13_psnsun",rc13_psnsun)

#var.def.nc(fileoutput,"rc13_psnsha","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "rc13_psnsha","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "rc13_psnsha",rc13_psnsha)

#var.def.nc(fileoutput,"leafc_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "leafc_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "leafc_13",leafc_13)

#var.def.nc(fileoutput,"leafc_storage_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "leafc_storage_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "leafc_storage_13",leafc_storage_13)

#var.def.nc(fileoutput,"leafc_xfer_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "leafc_xfer_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "leafc_xfer_13",leafc_xfer_13)

#var.def.nc(fileoutput,"frootc_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "frootc_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "frootc_13",frootc_13)

#var.def.nc(fileoutput,"frootc_storage_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "frootc_storage_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "frootc_storage_13",frootc_storage_13)

#var.def.nc(fileoutput,"frootc_xfer_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "frootc_xfer_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "frootc_xfer_13",frootc_xfer_13)

#var.def.nc(fileoutput,"livestemc_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "livestemc_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "livestemc_13",livestemc_13)

#var.def.nc(fileoutput,"livestemc_storage_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "livestemc_storage_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "livestemc_storage_13",livestemc_storage_13)

#var.def.nc(fileoutput,"livestemc_xfer_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "livestemc_xfer_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "livestemc_xfer_13",livestemc_xfer_13)

#var.def.nc(fileoutput,"deadstemc_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "deadstemc_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "deadstemc_13",deadstemc_13)

#var.def.nc(fileoutput,"deadstemc_storage_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "deadstemc_storage_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "deadstemc_storage_13",deadstemc_storage_13)

#var.def.nc(fileoutput,"deadstemc_xfer_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "deadstemc_xfer_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "deadstemc_xfer_13",deadstemc_xfer_13)

#var.def.nc(fileoutput,"livecrootc_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "livecrootc_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "livecrootc_13",livecrootc_13)

#var.def.nc(fileoutput,"deadstemc_13","NC_FLOAT",c("pft"))
#att.put.nc(fileoutput, "deadstemc_13","missing_value", "NC_FLOAT", -9999)
#var.put.nc(fileoutput, "deadstemc_13",deadstemc_13)

att.put.nc(fileoutput,"NC_GLOBAL","title","NC_CHAR","isotopic 13 carbon pools and 14 carbon pools")

close.nc(fileinput)
close.nc(fileoutput)


#' ---
#' title: "Prior probabilities in the interpretation of 'some': analysis of uniform prior wonky world model predictions"
#' author: "Judith Degen"
#' date: "January 26, 2014"
#' ---

library(ggplot2)
theme_set(theme_bw(18))
setwd("/Users/titlis/cogsci/projects/stanford/projects/thegricean_sinking-marbles/writing/_2015/_journal_cognition/pics/")
source("../rscripts/helpers.r")

# get prior modes
priormodes = read.table(file="~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/experiments/12_sinking-marbles-prior15/results/data/modes.txt",sep="\t", header=T, quote="")
row.names(priormodes) = paste(priormodes$Item)

# get prior expectations
priorexpectations = read.table(file="~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/experiments/24_sinking-marbles-prior-fourstep/results/data/expectations.txt",sep="\t", header=T, quote="")
row.names(priorexpectations) = priorexpectations$Item

# histogram of expectations
exps = ggplot(priorexpectations, aes(x=expectation_corr)) +
  geom_histogram() +
  scale_x_continuous(name="Expected value of prior distribution",breaks=seq(1,15, by=2)) +
  scale_y_continuous(name="Number of cases",breaks=seq(0,8, by=2))
ggsave("priorexpectations-histogram.pdf",width=5,height=3.7)

# get prior allstate-probs
priorprobs = read.table(file="~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/experiments/24_sinking-marbles-prior-fourstep/results/data/smoothed_15marbles_priors_withnames.txt",sep="\t", header=T, quote="")
row.names(priorprobs) = priorprobs$Item
head(priorprobs)

# histogram of allstate-probs
allprobs = ggplot(priorprobs, aes(x=X15)) +
  geom_histogram() +
  scale_x_continuous(name="Prior all-state probability") +
  scale_y_continuous(name="Number of cases")
ggsave("priorallprobs-histogram.pdf")

library(gridExtra)
pdf("priordistributions.pdf",width=10,height=4)
grid.arrange(exps, allprobs, nrow=1,widths=unit.c(unit(.5, "npc"), unit(.5, "npc")))
dev.off()

#####################################
# plot model predictions: expectations
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/models/wonky_world/results/data/mp-uniform.RData")

# plot expectations for unreliable speaker model:
toplot = droplevels(subset(mp, Quantifier == "some"))
nrow(toplot)
head(toplot)
summary(toplot)
ub = droplevels(subset(toplot, State == 15))

ggplot(ub, aes(x=PriorProbability, y=PosteriorProbability, color=as.factor(SpeakerOptimality))) +
  geom_point() +#color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth() +#color="#00B0F6") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,1), name="Prior all-state probability") +
  scale_y_continuous(limits=c(0,1), name="Model predicted posterior all-state probability") +
  facet_wrap(~WonkyWorldPrior)
ggsave("speakerreliability-ieoptimality.pdf",height=8,width=11)

# plot expectations for best basic model:
toplot = droplevels(subset(mp, QUD == "how-many" & Alternatives == "0_basic" & Quantifier == "some" & WonkyWorldPrior == .5))
nrow(toplot)
pexpectations = ddply(toplot, .(Item), summarise, PosteriorExpectation_predicted=sum(State*PosteriorProbability)/15)
head(pexpectations)
some = pexpectations#droplevels(subset(pexpectations, Quantifier == "some"))

toplot = droplevels(subset(some, SpeakerOptimality == 2))
nrow(toplot)
head(toplot)
toplot$Mode = priormodes[as.character(toplot$Item),]$Mode

ggplot(toplot, aes(x=PriorExpectation_smoothed, y=PosteriorExpectation_predicted)) +
  geom_point(color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth(color="#00B0F6") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,1), name="Prior expectation") +
  scale_y_continuous(limits=c(0,1), name="Model predicted posterior expectation")

pexpectations = ddply(toplot, .(Item, SpeakerOptimality,PriorExpectation_smoothed, PosteriorExpectation_empirical), summarise, PosteriorExpectation_predicted=sum(State*PosteriorProbability)/15)
head(pexpectations)
some = pexpectations#droplevels(subset(pexpectations, Quantifier == "some"))

toplot = droplevels(subset(some, SpeakerOptimality == 2))
nrow(toplot)
head(toplot)
toplot$Mode = priormodes[as.character(toplot$Item),]$Mode

ggplot(toplot, aes(x=PriorExpectation_smoothed, y=PosteriorExpectation_predicted)) +
  geom_point(color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth(color="#00B0F6") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,1), name="Prior expectation") +
  scale_y_continuous(limits=c(0,1), name="Model predicted posterior expectation")
#  geom_text(data=cors, aes(label=r)) +

#scale_size_discrete(range=c(1,2)) +
#scale_color_manual(values=c("red","blue","black"))
ggsave("model-expectations.pdf",width=5.5,height=4.5)#,width=30,height=10)
save(toplot, file="../data/toplot-expectations.RData")

# plot by prior mode instead
ggplot(toplot, aes(x=Mode, y=PosteriorExpectation_predicted)) +
  geom_point(color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth(color="#00B0F6") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,15), name="Prior mode") +
  scale_y_continuous(limits=c(0,1), name="Model predicted posterior expectation")

toplot_w = toplot
# get rRSA predictions for qud=how-many, alts=0_basic, spopt=2
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/models/complex_prior/smoothed_unbinned15/results/data/toplot-expectations.RData")
toplot_r = toplot
head(toplot_w)
summary(toplot_r)
toplot_r$Model = "RSA"
toplot_w$Model = "wRSA"

# plot regular rsa predictions
ggplot(toplot_r, aes(x=PriorExpectation_smoothed*15, y=PosteriorExpectation_predicted*15)) +
  geom_point(color="#007fb1") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth(color="#007fb1") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,15), breaks=seq(1,15,by=2), name="Prior expected number of objects") +
  scale_y_continuous(limits=c(0,15), breaks=seq(1,15,by=2), name="Posterior predicted number of objects")
#  geom_text(data=cors, aes(label=r)) +

#scale_size_discrete(range=c(1,2)) +
#scale_color_manual(values=c("red","blue","black"))
ggsave("model-expectations-rrsa.pdf",width=6.5,height=4.5)#,width=30,height=10)

# plot both rRSA and uniform wRSA expectation predictions in same plot
toplot = merge(toplot_r,toplot_w, all=T)
head(toplot)
nrow(toplot)
p_exps = ggplot(toplot, aes(x=PriorExpectation_smoothed*15, y=PosteriorExpectation_predicted*15, color=Model)) +
  geom_point() + #color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth() + #color="#00B0F6") +
  scale_color_manual(values=c("#007fb1", "#4ecdff")) +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,15), breaks=seq(1,15,by=2), name="Prior expected number of objects") +
  scale_y_continuous(limits=c(0,15), breaks=seq(1,15,by=2), name="Posterior predicted number of objects")
#  geom_text(data=cors, aes(label=r)) +
#scale_size_discrete(range=c(1,2)) +
#scale_color_manual(values=c("red","blue","black"))
#ggsave("graphs/model-expectations.pdf",width=5.5,height=4.5)#,width=30,height=10)
ggsave("model-expectations-binomial-regular.pdf",width=6.5,height=4.5)
ggsave("model-expectations-uniform-regular.pdf",width=6.5,height=4.5)

# plot model predictions: allstate-probs
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/models/wonky_world/results/data/mp-uniform.RData")
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/models/wonky_world/results/data/mp-binomial.RData")

# plot expectations for best basic model:
toplot = droplevels(subset(mp, QUD == "how-many" & Alternatives == "0_basic" & Quantifier == "some" & WonkyWorldPrior == .5 & State == 15))
nrow(toplot)

# adjust speaker optimality at will
toplot = droplevels(subset(toplot, SpeakerOptimality == 2))
nrow(toplot)
head(toplot)

ggplot(toplot, aes(x=PriorProbability, y=PosteriorProbability)) +
  geom_point(color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth(color="#00B0F6") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,1), name="Prior probability of all-state") +
  scale_y_continuous(limits=c(0,1), name="Model predicted posterior probability of all-state")
#  geom_text(data=cors, aes(label=r)) +

#scale_size_discrete(range=c(1,2)) +
#scale_color_manual(values=c("red","blue","black"))
ggsave("model-allprobs.pdf",width=5.5,height=4.5)#,width=30,height=10)
ggsave("model-uniform-allprobs.pdf",width=5.5,height=4.5)#,width=30,height=10)
save(toplot, file="../data/toplot-allprobs.RData")

toplot_w = toplot
# get rRSA predictions for qud=how-many, alts=0_basic, spopt=2
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/models/complex_prior/smoothed_unbinned15/results/data/mp.RData")
summary(mp)
toplot = droplevels(subset(mp, QUD == "how-many" & Alternatives == "0_basic" &  State == 15))
nrow(toplot)

# adjust speaker optimality at will
toplot = droplevels(subset(toplot, SpeakerOptimality == 2))
nrow(toplot)
head(toplot)

toplot_r = toplot

# plot only rrsa
ggplot(toplot_r, aes(x=PriorProbability, y=PosteriorProbability)) +
  geom_point(color="#007fb1") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth(color="#007fb1") + #color="#00B0F6") +
  #scale_color_manual(values=c("#007fb1")) +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,1), name="Prior probability of all-state") +
  scale_y_continuous(limits=c(0,1), name="Model predicted posterior probability of all-state")
#  geom_text(data=cors, aes(label=r)) +
#scale_size_discrete(range=c(1,2)) +
#scale_color_manual(values=c("red","blue","black"))
#ggsave("graphs/model-expectations.pdf",width=5.5,height=4.5)#,width=30,height=10)
ggsave("model-allprobs-rrsa.pdf",width=6.5,height=4.5)

head(toplot_w)
summary(toplot_r)
toplot_r$Model = "RSA"
toplot_w$Model = "wRSA"

# plot both rRSA and uniform wRSA expectation predictions in same plot
toplot = merge(toplot_r,toplot_w, all=T)
head(toplot)
nrow(toplot)
p_probs = ggplot(toplot, aes(x=PriorProbability, y=PosteriorProbability, color=Model)) +
  geom_point() + #color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth() + #color="#00B0F6") +
  scale_color_manual(values=c("#007fb1", "#4ecdff")) +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_x_continuous(limits=c(0,1), name="Prior probability of all-state") +
  scale_y_continuous(limits=c(0,1), name="Model predicted posterior probability of all-state")
#  geom_text(data=cors, aes(label=r)) +
#scale_size_discrete(range=c(1,2)) +
#scale_color_manual(values=c("red","blue","black"))
#ggsave("graphs/model-expectations.pdf",width=5.5,height=4.5)#,width=30,height=10)
ggsave("model-allprobs-binomial-regular.pdf",width=6.5,height=4.5)
ggsave("model-allprobs-uniform-regular.pdf",width=6.5,height=4.5)

library(gridExtra)
#  share a legend between multiple plots
g <- ggplotGrob(p_exps + theme(legend.position="right"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
p_exps_nolegend = p_exps + theme(legend.position="none")
p_probs_nolegend = p_probs + theme(legend.position="none")

#pdf("rsa-predictions-uniform.pdf",width=10,height=4)
pdf("rsa-predictions.pdf",width=10,height=4)
grid.arrange(p_exps_nolegend, p_probs_nolegend, legend,nrow=1,widths=unit.c(unit(.45, "npc"), unit(.45, "npc"), unit(.1, "npc")))
dev.off()

s = subset(r, quantifier=="Some" & Proportion == "100")
nrow(s)

library(lmerTest)
m=lmer(normresponse ~ AllPriorProbability + (1+AllPriorProbability|workerid) + (1|Item), data=s)
summary(m)

#####################################
# plot model predictions: wonkiness
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/models/wonky_world/results/data/wr-uniform.RData")
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/models/wonky_world/results/data/wr-binomial.RData")

# plot expectations for best basic model:
toplot = droplevels(subset(wr, WonkyWorldPrior == .5 & Wonky == "true"))
nrow(toplot)
toplot$Mode = priormodes[as.character(toplot$Item),]$Mode

toplot = droplevels(subset(toplot, SpeakerOptimality == 2))
mod = toplot
nrow(toplot)
head(toplot)

p_wmodel = ggplot(toplot, aes(x=PriorExpectation, y=PosteriorProbability,color=Quantifier)) +
  geom_point() + #color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth() + #color="#00B0F6") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_color_manual(values=c("#F8766D","#00BF7D","#00B0F6")) +
  scale_x_continuous(breaks=seq(1,15,by=2),name="Prior expected number of objects") +
  scale_y_continuous(breaks=seq(0,1,by=.25),name="Model predicted wonkiness probability")

ggsave("model-wonkiness-uniform.pdf",width=5.5,height=4)#,width=30,height=10)
ggsave("model-wonkiness-binomial.pdf",width=5.5,height=4)#,width=30,height=10)

# plot by mode
ggplot(toplot, aes(x=Mode, y=PosteriorProbability,color=Quantifier)) +
  geom_point() + #color="#00B0F6") + #values=c("#F8766D", "#A3A500", "#00BF7D", "#E76BF3", "#00B0F6")
  geom_smooth() + #color="#00B0F6") +
  #  geom_abline(intercept=0,slope=1,color="gray50") +
  scale_color_manual(values=c("#F8766D","#00BF7D","#00B0F6")) +
  scale_x_continuous(breaks=seq(1,15,by=2),name="Prior expected number of objects") +
  scale_y_continuous(breaks=seq(0,1,by=.25),name="Model predicted wonkiness probability")

############
# empirical wonkiness posteriors
############
load("/Users/titlis/cogsci/projects/stanford/projects/thegricean_sinking-marbles/experiments/17_sinking-marbles-normal-sliders/results/data/r.RData")
head(r)
nrow(r)

ggplot(r, aes(x=response, fill=quantifier)) +
  geom_histogram() +
  facet_grid(workerid~quantifier)
ggsave("subject-variability-wonkiness.pdf",width=7,height=25)

toplot = aggregate(response ~ quantifier + Item + PriorExpectation, FUN="mean", data=r)
toplot = droplevels(subset(toplot, quantifier %in% c("All","Some","None")))
toplot$Quantifier = factor(tolower(toplot$quantifier), levels=c("all", "none","some"))
toplot$Mode = priormodes[as.character(toplot$Item),]$Mode

p_wempirical = ggplot(toplot, aes(x=PriorExpectation, y=response, color=Quantifier)) +
  geom_point() +
  geom_smooth() +
  scale_color_manual(values=c("#F8766D","#00BF7D","#00B0F6")) +
  scale_x_continuous(breaks=seq(1,15,by=2),name="Prior expected number of objects") +
  scale_y_continuous(breaks=seq(0,1,by=.25),name="Mean empirical wonkiness probability")
ggsave(file="empirical-wonkiness.pdf",width=5.5,height=4)

library(gridExtra)
#  share a legend between multiple plots
g <- ggplotGrob(p_wmodel + theme(legend.position="right"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
p_wmodel_nolegend = p_wmodel + theme(legend.position="none")
p_wempirical_nolegend = p_wempirical + theme(legend.position="none")

#pdf("rsa-predictions-uniform.pdf",width=10,height=4)
pdf("wonkiness-fullplot.pdf",width=12,height=5)
grid.arrange(p_wmodel_nolegend, p_wempirical_nolegend, legend,nrow=1,widths=unit.c(unit(.45, "npc"), unit(.45, "npc"), unit(.1, "npc")))
dev.off()

# scatterplot of mdoel vs empirical
head(toplot)
toplot$PosteriorProbability = toplot$response
head(mod)
toplot$Type = "empirical"
mod$Type = "model"

pl = merge(mod, toplot, all=T)
summary(pl)
pl$Type = as.factor(as.character(pl$Type))
topl = pl %>% select(Item, Quantifier,PosteriorProbability,Type) %>%
  spread(Type,PosteriorProbability)
summary(topl)

ggplot(topl, aes(x=model,y=empirical,color=Quantifier,group=1)) +
  geom_point() +
  geom_smooth(method="lm") +
  scale_color_manual(values=c("#F8766D","#00BF7D","#00B0F6"))
cor(topl$model,topl$empirical,use="pairwise.complete.obs") # cor=.69

library(hydroGOF)
gof(topl$model,topl$empirical,na.rm=T,do.spearman=T)

#plot by mode
ggplot(toplot, aes(x=Mode, y=response, color=Quantifier)) +
  geom_point() +
  geom_smooth() +
  scale_color_manual(values=c("#F8766D","#00BF7D","#00B0F6")) +
  scale_x_continuous(breaks=seq(1,15,by=2),name="Prior expected number of objects") +
  scale_y_continuous(breaks=seq(0,1,by=.25),name="Mean empirical wonkiness probability")

###############
## EMPIRICAL PLOTS
###############

# expectations
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/experiments/13_sinking-marbles-priordv-15/results/data/r.RData")

agr = aggregate(ProportionResponse ~ PriorExpectationProportion + quantifier + Item,data=r,FUN=mean)
#agr$CILow = aggregate(ProportionResponse ~ PriorExpectationProportion + quantifier + Item,data=r, FUN=ci.low)$ProportionResponse
#agr$CIHigh = aggregate(ProportionResponse ~ PriorExpectationProportion + quantifier + Item,data=r,FUN=ci.high)$ProportionResponse
#agr$YMin = agr$ProportionResponse - agr$CILow
#agr$YMax = agr$ProportionResponse + agr$CIHigh

min(agr[agr$quantifier == "Some",]$ProportionResponse)
max(agr[agr$quantifier == "Some",]$ProportionResponse)

p_eexps = ggplot(agr, aes(x=PriorExpectationProportion*15, y=ProportionResponse*15, color=quantifier)) +
  geom_point() +
  #geom_errorbar(aes(ymin=YMin,ymax=YMax)) +
  geom_smooth(method="lm") +
  scale_color_manual(values=c("#F8766D", "black", "#00BF7D", "gray30", "#00B0F6"),breaks=levels(agr$quantifier),labels=c("all","long filler","none","short filler","some")) +
  scale_y_continuous(breaks=seq(1,15,by=2),name="Posterior mean number of objects") +
  #  geom_abline(intercept=0,slope=1,color="gray70") +
  scale_x_continuous(breaks=seq(1,15,by=2),name="Prior mean number of objects")
ggsave(file="meanresponses.pdf",width=5,height=3.7)

# allstate-probs
load("~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/experiments/16_sinking-marbles-sliders-certain/results/data/r.RData")

# exclude people who are doing some sort of bullshit and not responding reasonably to all/none (see subject-variability.pdf for behavior on zero-slider)
tmp = subset(r,!workerid %in% c(0,22,43,98,100,103,117,118))
agrr = aggregate(normresponse ~ AllPriorProbability + Proportion + quantifier + Item,data=tmp,FUN=mean)
agrr = aggregate(normresponse ~ AllPriorProbability + Proportion + quantifier + Item,data=r,FUN=mean)
ub = subset(agrr, Proportion == "100")
ub = droplevels(ub)

p_eprobs = ggplot(ub, aes(x=AllPriorProbability, y = normresponse, color=quantifier)) +
  geom_point() +
  geom_smooth(method="lm") +
  scale_color_manual(values=c("#F8766D", "black", "#00BF7D", "gray30", "#00B0F6"),breaks=levels(agrr$quantifier),labels=c("all","long filler","none","short filler","some")) +
  scale_y_continuous(limits=c(0,1),name="Posterior probability of all-state ") +
  #  geom_abline(intercept=0,slope=1,color="gray70") +
  scale_x_continuous(limits=c(0,1),name="Prior probability of all-state")
ggsave(file="empirical-allprobs.pdf",width=5,height=3.7)

library(gridExtra)
#  share a legend between multiple plots
g <- ggplotGrob(p_eexps + theme(legend.position="right"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
p_eexps_nolegend = p_eexps + theme(legend.position="none")
p_eprobs_nolegend = p_eprobs + theme(legend.position="none")

pdf("empirical-results.pdf",width=10,height=4)
grid.arrange(p_eexps_nolegend, p_eprobs_nolegend, legend,nrow=1,widths=unit.c(unit(.45, "npc"), unit(.45, "npc"), unit(.1, "npc")))
dev.off()

# plot subejct variability for exclusion
ggplot(r[r$Proportion == "0",], aes(x=normresponse,fill=quantifier)) +
  geom_histogram() +
  facet_grid(workerid~quantifier)
ggsave("subject-variability.pdf",width=5.5,height=4)

######################
### NOAH'S MODEL PREDICTIONS
rsa_allstate = read.csv(file="~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/ndg-code/RSA-allstate.csv",header=F)
head(rsa_allstate)
colnames(rsa_allstate) = c("BinomialCW","PosteriorAllProbability")
ggplot(rsa_allstate, aes(x=BinomialCW,y=PosteriorAllProbability)) +
  geom_point(color="#00B0F6") +
  scale_x_continuous(limits=c(0,1), name="Binomial coin weight") +
  scale_y_continuous(limits=c(0,1), name="Predicted posterior probability of all-state")
ggsave("noahs-allstate-predictions.pdf",width=4.5,height=3.5)

rsa_expectation = read.csv(file="~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/ndg-code/RSA-expectation.csv",header=F)
head(rsa_expectation)
colnames(rsa_expectation) = c("PriorExpectation","PosteriorExpectation")
ggplot(rsa_expectation, aes(x=PriorExpectation/15,y=PosteriorExpectation/15)) +
  geom_point(color="#00B0F6") +
  scale_x_continuous(limits=c(0,1), name="Prior expectation") +
  scale_y_continuous(limits=c(0,1), name="Predicted posterior expectation")
ggsave("noahs-expectation-predictions.pdf",width=4.5,height=3.5)

rsa_expall = read.csv(file="~/cogsci/projects/stanford/projects/thegricean_sinking-marbles/ndg-code/RSA-expectedVsallstate.csv",header=F)
head(rsa_expall)
colnames(rsa_expall) = c("PriorExpectation","PosteriorExpectation")
ggplot(rsa_expall, aes(x=PriorExpectation/15,y=PosteriorExpectation)) +
  geom_point(color="#00B0F6") +
  scale_x_continuous(limits=c(0,1), name="Prior expectation") +
  scale_y_continuous(limits=c(0,1), name="Predicted posterior probability of all-state")
ggsave("noahs-allstate-predictions-bypriorexp.pdf",width=4.5,height=3.5)


subroutine inddup(x,y,n,rw,frac,dup)
implicit double precision(a-h,o-z)
logical dup(n)
dimension x(n), y(n), rw(4)

xtol = frac*(rw(2)-rw(1))
ytol = frac*(rw(4)-rw(3))

dup(1) = .false.
do i = 2,n {
	dup(i) = .false.
	do j = 1,i-1 {
		dx = abs(x(i)-x(j))
		dy = abs(y(i)-y(j))
		if(dx < xtol & dy < ytol) {
			dup(i) = .true.
			break
		}
	}
}

return
end


#' barefootr: A package for holding useful functions for Eric
#'
#' I use this package to hold some functions that are useful to me.
#'
#' @section barefootr functions:
#'
#' The functions here make up a mix of tools to calculate and estimate modern suficial processes and back-calculate those processes in ancient rivers.
#'
#' \code{settle} calculates the settling velocity of particles following the formulation by Dietrich (1982).
#'
#' \code{lynds_one} estimates ancient river slope using the first method described by Lynds et al. (2014)
#'
#' \code{lynds_two} estimates ancient river slope using the second method described by Lynds et al. (2014)
#'
#' \code{lynds_three} estimates ancient river slope using the third method described by Lynds et al. (2014)
#'
#' \code{xset2H} estimates ancient river dune heights based on cross-set thicknesses. Methods from Leclair and Paola.
#'
#' \code{H2h} estimates ancient river depths based on dune heights. Methods from Bradley and Venditti.
#'
#' \code{xset2depthSimple} estimates ancient river depths directly from cross-set thicknesses using the simplest assumptions. Methods from Leclair, Paola, and Bradley and Venditti.
#'
#' \code{azStringConvert} Azimuth conversion for 'quadrant' based strike/trend measurements.
#'
#'
#'
#'
#'
#'
#'
#' @docType package
#' @name barefootr
NULL


# The MIT License (MIT)
# Copyright (c) 2017 Louise AC Millard, MRC Integrative Epidemiology Unit, University of Bristol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without
# limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions
# of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# splits the pheno into 3 bins with the cut points between values rather at the exact value for the quantile
equalSizedBins <- function(phenoAvg) {

	## equal sized bins
        q = quantile(phenoAvg, probs=c(1/3,2/3), na.rm=TRUE)

	minX = min(phenoAvg, na.rm=TRUE)
	maxX = max(phenoAvg, na.rm=TRUE)

	phenoBinned = phenoAvg;
	if (q[1]==minX) {
		# edge case - quantile value is lowest value

		# assign min value as cat1
		idx1 = which(phenoAvg==q[1]);
		phenoBinned[idx1] = 0;

		# divide remaining values into cat2 and cat3
		phenoAvgRemaining = phenoAvg[which(phenoAvg!=q[1])];
		qx = quantile(phenoAvgRemaining, probs=c(0.5), na.rm=TRUE)
		minXX = min(phenoAvgRemaining, na.rm=TRUE)
		maxXX =	max(phenoAvgRemaining, na.rm=TRUE)

		if (qx[1]==minXX) {
			# edge case again - quantile value is lowest value
			idx2 = which(phenoAvg==qx[1]);
			idx3 = which(phenoAvg>qx[1]);
			cat("Bin 1: ==", q[1],", bin 2: ==",qx[1], "bin 3: >", qx[1], " || ", sep="")
		}
		else if (qx[1]==maxXX) {
			# edge case again - quantile value is max value
			idx2 = which(phenoAvg<qx[1] & phenoAvg>q[1]);
			idx3 = which(phenoAvg==qx[1]);
			cat("Bin 1: ==", q[1],", bin 2: > ",q[1], " AND <", qx[1] , "bin 3: ==", qx[1], " || ", sep="")
		}
		else {
			idx2 = which(phenoAvg<qx[1] & phenoAvg>q[1]);
			idx3 = which(phenoAvg>=qx[1]);
			cat("Bin 1: ==", q[1],", bin 2: > ",q[1], " AND <", qx[1] , "bin 3: >=", qx[1], " || ", sep="")
		}
		phenoBinned[idx2] = 1;
                phenoBinned[idx3] = 2;
	}
	else if (q[2]==maxX) {
		# edge case - quantile value is highest value

		# assign max value as cat3
		idx3 = which(phenoAvg==q[2]);
		phenoBinned[idx3] = 2;

		# divide remaining values into cat1 and cat2
		phenoAvgRemaining = phenoAvg[which(phenoAvg!=q[2])];
                qx = quantile(phenoAvgRemaining, probs=c(0.5), na.rm=TRUE)
		minXX = min(phenoAvgRemaining, na.rm=TRUE)
                maxXX = max(phenoAvgRemaining, na.rm=TRUE)

		if (qx[1]==minXX) {
			# edge case again - quantile value is lowest value
                        idx1 = which(phenoAvg==qx[1]);
                        idx2 = which(phenoAvg>qx[1] & phenoAvg<q[2]);
			cat("Bin 1: ==", qx[1], ", bin 2: >", qx[1], " AND < ", q[2], ", bin 3: ==", q[2], " || ", sep="")
                }
                else if	(qx[1]==maxXX) {
			# edge case again - quantile value is max value
                        idx1 = which(phenoAvg<qx[1]);
                        idx2 = which(phenoAvg==qx[1]);
			cat("Bin 1: <", qx[1], ", bin 2: ==", qx[1], ", bin 3: ==", q[2], " || ", sep="")
                }
                else {
	                idx1 = which(phenoAvg<qx[1]);
			idx2 = which(phenoAvg>=qx[1] & phenoAvg<q[2]);
			cat("Bin 1: <", qx[1], ", bin 2: >=", qx[1], " AND < ", q[2], ", bin 3: ==", q[2], " || ", sep="")
		}

                phenoBinned[idx1] = 0;
                phenoBinned[idx2] = 1;
	}
       else if (q[1] == q[2]) {
		# both quantiles correspond to the same value so set
		# cat1 as < this value, cat2 as exactly this value and
		# cat3 as > this value
              	phenoBinned = phenoAvg;
        	idx1 = which(phenoAvg<q[1]);
                idx2 = which(phenoAvg==q[2]);
                idx3 = which(phenoAvg>q[2]);
                phenoBinned[idx1] = 0;
                phenoBinned[idx2] = 1;
                phenoBinned[idx3] = 2;

		cat("Bin 1: <", q[1], ", bin 2: ==", q[2], ", bin 3: >", q[2], " || ", sep="")
       	}
        else {
		# standard case - split the data into three roughly equal parts where
		# cat1<q1, cat2 between q1 and q2, and cat3>=q2
         	phenoBinned = phenoAvg;
                idx1 = which(phenoAvg<q[1]);
                idx2 = which(phenoAvg>=q[1] & phenoAvg<q[2]);
               	idx3 = which(phenoAvg>=q[2]);
                phenoBinned[idx1] = 0;
                phenoBinned[idx2] = 1;
                phenoBinned[idx3] = 2;

		cat("Bin 1: <", q[1], ", bin 2: >=", q[1], "AND < ", q[2] ,", bin 3: >=", q[2], " || ", sep="")
	}

	cat("cat N: ", length(idx1),", ",length(idx2),", ",length(idx3), " || ", sep="");

	return(phenoBinned);
}


#R Vignere Functions Document

alphabet <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z")

english_frequencies <- data.frame(
    letters = alphabet,
    frequencies = c(8.4966, 2.0720, 4.5388, 3.3844, 11.1607, 1.8121, 2.4705,
    3.0034, 7.5448, 0.1965, 1.1016, 5.4893, 3.0129, 6.6544, 7.1635, 3.1671,
    0.1962, 7.5809, 5.7351, 6.9509, 3.6308, 1.0074, 1.2899, 0.2902, 1.7779,
    0.2722) / 100
)

#Removes all punctuation, upper case letters and spaces from a string
format_func <- function(string) {
    string <- gsub("[[:punct:][:blank:]]", "", string)
    string <- gsub("[0-9]", "", string)
    string <- tolower(string)
    return(string)
}

#Function returns the encrypted text by applying the vigenere cipher
#Function requires a key, and a string, returns the ciphertext
encrypt_func <- function(text, key) {
    encrypted <- c()
    #loop to get alphabetical indexes of the characters in the text
    split_text <- strsplit(text, "")[[1]] #creates a character class
    for (i in 1:nchar(text)) {
        letter_index <- which(alphabet %in% split_text[i])
        j <- i %% nchar(key)
        if (j == 0) {
            j <- nchar(key)
        }
        key_index <- which(alphabet %in% substring(key, j, j))
        #applying the relevant shift to the letters, using the vigenere cipher
        alphabet_index <- (letter_index + key_index - 2) %% length(alphabet) + 1
        encrypted <- paste(encrypted, alphabet[alphabet_index], sep = "")
    }
    return(encrypted)
}

decrypt <- function(ciphertext, key) {
    decrypted <- c()
    for (i in 1:nchar(ciphertext)) {
        j <- i %% nchar(key)
        if (j == 0) {
            j <- nchar(key)
        }
        key_index <- which(alphabet %in% substring(key, j, j))
        cipher_index <- which(alphabet %in% substring(ciphertext, i, i))
        alphabet_index <- (cipher_index - key_index) %% 26 + 1
        decrypted <- paste(decrypted, alphabet[alphabet_index], sep = "")
    }
    return(decrypted)
}

#Kasiski Algorithm Functions to find the key length

#Repeats function, finds all n letter repeats within the ciphertext
#   then counts their frequency, finds the spacing between repeats,
#   then finds the most comming factor of all the spacings (kas_mode)
repeats <- function(ciphertext, n) {
    elements <- c()
    indexes <- list()
    spacings <- list()
    for (i in 1:nchar(ciphertext)) {
        if (i <= nchar(ciphertext) - n + 1) {
            substr <- substring(ciphertext, i, i + n - 1)
            if (substr %in% elements == FALSE) {
                elements <- c(elements, substr)
                data <- gregexpr(substr, ciphertext)
                data2 <- list("attributes<-" (data[[1]], NULL))
                indexes <- append(indexes, data2)
                if (length(data2[[1]]) > 1) {
                    diff <- c()
                    for (j in 1:(length(data2[[1]]) - 1)) {
                        for (k in 1:(length(data2[[1]]) - j)) {
                            diff <- c(diff, data2[[1]][j + k] - data2[[1]][j])
                        }
                    }
                    spacings <- append(spacings, diff)
                }
            }
        }
    }
    factors_matrix <- c()
    for (i in 2:nchar(ciphertext)) {
        counts <- 0
        for (d in spacings) {
            if ((d %% i) == 0) {
                counts <- counts + 1
            }
        }
        if (counts != 0) {
            factors_matrix <- rbind(factors_matrix, c(i, counts))
        }
    }
    kas_mode <- 0
    mode_val <- 0
    for (r in 1:nrow(factors_matrix)) {
        if (factors_matrix[r, 2] >= mode_val) {
            kas_mode <- factors_matrix[r, 1]
            mode_val <- factors_matrix[r, 2]
        }
    }
    return(kas_mode)
}

#Function takes every nth letter starting from index m,
#where m is in range 0:n-1
str_subset <- function(junk, n, m) {
   ifelse(n == 1, junk,
paste(strsplit(junk, split = NULL)[[1]][(1:nchar(junk)) %% n == m],
collapse = ""))
}

#Function below returns a list of the ciphertext split into n=key_length
#subsections, so that letter frequency analysis can be used
nth_letter <- function(key_length, ciphertext) {
    list_of_substrings <- c()
    sub_indexes <- 1:(key_length - 1)
    sub_indexes <- append(sub_indexes, 0)
    if (key_length == 1) {
        sub_indexes <- c(1)
    }
    for (i in sub_indexes) {
        list_of_substrings <- append(list_of_substrings,
        str_subset(ciphertext, key_length, i))
    }
    return(list_of_substrings)
}

sub_letter_totals <- function(key_length, ciphertext) {
    sorted_frequencies <- c()
    list_of_substrings <- nth_letter(key_length, ciphertext)
    index <- 1
    for (sub in list_of_substrings) {
        letter_freqs <- c()
        sub_split <- strsplit(sub, "")[[1]]
        for (letter in alphabet) {
            count <- length(grep(letter, sub_split))
            letter_freqs <- append(letter_freqs, count)
        }
        sorted_frequencies[[index]] <- letter_freqs
        index <- index + 1
    }
    return(sorted_frequencies)
}

percentages <- function(vec_of_vecs) {
    int <- 1
    s_percs <- c()
    for (i in vec_of_vecs) {
        j <- i / sum(i)
        s_percs[[int]] <- j
        int <- int + 1
    }
    return(s_percs)
}

chi_squared <- function(eng_freqs, sub_percs) {
    chis <- c()
    ind <- 1
    for (sublist in sub_percs) {
        chi_stats <- c()
        for (i in 1:length(alphabet)) {
            chi <- 0
            if (i == 1) {
                series <- i:26
            }
            else {
                series <- (28 - i):26
                series <- c(series, 1:(27 - i))
            }
            for (j in seq_along(sublist)) {
                value <- ((sublist[j] - eng_freqs[[2]][series[j]]) ^ 2) / (eng_freqs[[2]][series[j]])
                chi <- chi + value
            }
            chi_stats <- c(chi_stats, chi)
        }
        chis[[ind]] <- chi_stats
        ind <- ind + 1
    }
    return(chis)
}

#Function which returns the n most likely letters for every key letter.
#It takes in two arguments, the vector list of chi squared values, and n.
#It returns a vector, of length == key length.
likely_letters <- function(chi_vector, n) {
    n_likely <- c()
    step <- 1
    for (chis in chi_vector) {
        letters <- c()
        sorted_freqs <- chis[order(chis)]
        for (int in 1:n) {
            letters <- c(letters, alphabet[match(sorted_freqs[int], chis)])
        }
        n_likely[[step]] <- letters
        step <- step + 1
    }
    return(n_likely)
}

### Twist Algorithm Implementation, used to find the key length

twist_subsets_func <- function(key_limit, text) {
    all_key_lengths <- c()
    for (m in 1:(key_limit + 1)) {
        nth_sub <- sub_letter_totals(m, text)
        perc <- percentages(nth_sub)
        all_key_lengths[[m]] <- perc
    }
    return(all_key_lengths)
}

twists <- function(twist_subsets) {
    twists <- c()
    for (c in 1:length(twist_subsets)) {
        twist <- 0
        for (r in 1:length(twist_subsets[[c]])) {
            list_of_percs <- sort(twist_subsets[[c]][r][[1]], decreasing = TRUE)
            twist <- twist + sum(list_of_percs[1:13]) - sum(list_of_percs[14:26])
        }
        twist <- twist * (100 / c)
        twists <- c(twists, twist)
    }
    return(twists)
}

twistplus <- function(twists) {
    twistplus <- c()
    for (i in seq_along(twists)) {
        subtract <- 0
        if (i != length(twists)) {
            for (j in 1:i) {
                subtract <- subtract + (twists[j]/(i))
            }
            number <- twists[i + 1] - subtract
            twistplus <- c(twistplus, number, i + 1)
        }
    }
    mode_value <- 0
    sequence <- seq_along(twistplus)
    integers <- which(sequence %% 2 == 1)
    for (j in integers) {
        if(twistplus[j] > mode_value) {
            mode <- twistplus[j + 1]
            mode_value <- twistplus[j]
        }
    }
    return (mode)
}

############################
#Testing
############################

#Code below demonstrates how the Vigenere Cipher is solved with the twistplus algorithm
#to identify the key length.

normal_text <- "there are various kinds of certainty. a belief is psychologically certain when the subject who has it is supremely convinced of its truth. certainty in this sense is similar to incorrigibility, which is the property a belief has of being such that the subject is incapable of giving it up.but psychological certainty is not the same thing as incorrigibility. a belief can be certain in this sense without being incorrigible; this may happen, for example, when the subject receives a very compelling bit of counterevidence to the (previously) certain belief and gives it up for that reason."
keyword <- "notebook"

c_text <- encrypt_func(format_func(normal_text), keyword)
twists_a <- twists(twist_subsets_func(8, c_text))
mode <- twistplus(twists_a)
print(mode)

output <- sub_letter_totals(mode, c_text)
percs <- percentages(output)
chi_vals <- chi_squared(english_frequencies, percs)
likely <- likely_letters(chi_vals, 3)
print(likely)

decryp <- decrypt(c_text, "notebook")
print(decryp)

#######################
#Testing the Kasiski Algorithm
#######################

#Try different length ciphertexts and keys to determine the effectiveness of the Kasiski Algorithm
#compared to the Twistplus algorithm. You may find different results for the function repeats(text, n), when
#changing 'n' the length of the repeated fragment of text.
repeats(c_text, 5)


\name{psm_analysis_weighted}
\alias{psm_analysis_weighted}

\title{
Weighted van Westendorp Price Sensitivity Meter Analysis (PSM)
}

\description{
\code{psm_analysis_weighted()} performs a \bold{weighted} analysis
  of consumer price preferences and price sensitivity known as
  \bold{van Westendorp Price Sensitivity Meter (PSM)}. The function
  requires a sample design from the \pkg{survey} package as the
  main input. Custom weights or sample designs from other packages
  are not supported.

  To run a PSM analysis \bold{without} weighting, use the function
  \code{\link{psm_analysis}}.

}

\usage{
psm_analysis_weighted(
  toocheap, cheap, expensive, tooexpensive,
  design,
  validate = TRUE,
  interpolate = FALSE,
  interpolation_steps = 0.01,
  intersection_method = "min",
  pi_cheap = NA, pi_expensive = NA,
  pi_scale = 5:1,
  pi_calibrated = c(0.7, 0.5, 0.3, 0.1, 0))
}

\arguments{
  \item{toocheap, cheap, expensive, tooexpensive}{Names
  of the variables in the data.frame/matrix that contain the
  survey data on the respondents' "too cheap", "cheap",
  "expensive" and "too expensive" price preferences.

  If the \code{toocheap} price was not assessed, a
  variable of NAs can be used instead.  If \code{toocheap}
  is NA for all cases, it is possible to calculate the Point of
  Marginal Expensiveness and the Indifference Price Point, but it
  is impossible to calculate the Point of Marginal Cheapness and
  the Optimal Price Point.}
  \item{design}{A survey design which has been created by the
  function \code{\link{svydesign}()} from the \pkg{survey}
  package. The data that is used as an input of \code{svydesign()}
  must include all the variable names for \code{toocheap},
  \code{cheap}, \code{expensive} and \code{tooexpensive} variables
  specified above.}
  \item{validate}{logical. should only respondents with
  consistent price preferences (too cheap < cheap < expensive
  < too expensive) be considered in the analysis?}
  \item{interpolate}{logical. should interpolation of the price
  curves be applied between the actual prices given by the
  respondents? If interpolation is enabled, the output appears
  less bumpy in regions with sparse price information. If the
  sample size is sufficiently large, interpolation should not
  be necessary.}
  \item{interpolation_steps}{numeric. if \code{interpolate} is
  \code{TRUE}: the size of the interpolation steps. Set by
  default to 0.01, which should be appropriate for most goods
  in a price range of 0-50 USD/Euro.}
    \item{intersection_method}{"min" (default), "max", "mean" or
  "median". defines the method how to determine the price
  points (range, indifference price, optimal price) if there
  are multiple possible intersections of the price curves.
  "min" uses the lowest possible prices, "max" uses the
  highest possible prices, "mean" calculates the mean among
  all intersections and "median" uses the median of all
  possible intersections}
  \item{pi_cheap, pi_expensive}{Only required for the Newton
  Miller Smith extension. Names of the variables in the data
  that contain the survey data on the respondents' purchase
  intent at their individual cheap/expensive price.}
  \item{pi_scale}{Only required for the Newton Miller Smith
  extension. Scale of the purchase intent variables pi_cheap and
  pi_expensive. By default assuming a five-point scale with 5
  indicating the highest purchase intent.}
  \item{pi_calibrated}{Only required for the Newton Miller Smith
  extension. Calibrated purchase probabilities that are assumed
  for each value of the purchase intent scale. Must be the same
  order as the pi_scale variable so that the first value of
  pi_calibrated corresponds to the first value in the pi_scale
  variable. Default values are taken from the Sawtooth Software
  PSM implementation in Excel: 70\% for the best value of the
  purchase intent scale, 50\% for the second best value,
  30\% for the third best value (middle of the scale), 10\%
  for the fourth best value and 0\% for the worst value.}
}

\details{
The main logic of the Price Sensitivity Meter Analysis is
explained in the documentation of the \code{\link{psm_analysis}}
function. The \code{psm_analysis_weighted} performs the same
analysis, but weights the survey data according to a known
population.}

\value{
The function output consists of the following elements:

    \item{\code{data_input}:}{\code{data.frame} object. Contains
    the data that was used as an input for the analysis.}
    \item{\code{validated}:}{\code{logical} object. Indicates
    whether the \code{"validate"} option has been used (to
    exclude cases with intransitive price preferences).}
    \item{\code{invalid_cases}:}{\code{numeric} object. Number
    of cases with intransitive price preferences.}
    \item{\code{total_sample}:}{\code{"numeric"} object.
    Total sample size of the input sample \emph{before}
    assessing the transitivity of individual price preferences.}
    \item{\code{data_vanwestendorp}:}{\code{data.frame} object.
    Output data of the Price Sensitivity Meter analysis.
    Contains the \bold{weighted} cumulative distribution
    functions for the four price assessments (too cheap, cheap,
    expensive, too expensive) for all prices.}
    \item{\code{pricerange_lower}:}{\code{numeric} object. Lower
    limit of the acceptable price range as defined by the
    Price Sensitivity Meter, also known as \bold{point of
    marginal cheapness}: Intersection of the "too cheap" and the
    "expensive" curves.}
    \item{\code{pricerange_upper}:}{\code{numeric} object. Upper
    limit of the acceptable price range as defined by the Price
    Sensitivity Meter, also known as \bold{point of marginal
    expensiveness}: Intersection of the "too expensive" and the
    "cheap" curves.}
    \item{\code{idp}:}{\code{numeric} object. \bold{Indifference
    Price Point} as defined by the Price Sensitivity Meter:
    Intersection of the "cheap" and the "expensive" curves.}
    \item{\code{opp}:}{\code{numeric} object. \bold{Optimal
    Price Point} as defined by the Price Sensitivity Meter:
    Intersection of the "too cheap" and the "too expensive"
    curves.}
    \item{\code{weighted}:}{\code{logical} object. Indicating
    if weighted data was used in the analysis. Outputs from
    \code{psm_analysis_weighted()} always have the value
    \code{TRUE}. When data is unweighted, use the function
    \code{\link{psm_analysis}.}}
    \item{\code{survey_design}:}{\code{survey.design2} object.
    Returning the full survey design as specified with the
    \code{\link[survey]{svydesign}} function from the \pkg{survey} package.}
    \item{\code{NMS}:}{\code{logical} object. Indicates whether
    the additional analyses of the Newton Miller Smith Extension
    were performed.}
}

\references{
  Van Westendorp, P (1976) "NSS-Price Sensitivity Meter (PSM) --
  A new approach to study consumer perception of price"
  \emph{Proceedings of the ESOMAR 29th Congress}, 139--167. Online
  available at \url{https://www.researchworld.com/a-new-approach-to-study-consumer-perception-of-price/}.

  Newton, D, Miller, J, Smith, P, (1993) "A market acceptance
  extension to traditional price sensitivity measurement"
  \emph{Proceedings of the American Marketing Association
  Advanced Research Techniques Forum}.

  Sawtooth Software (2016) "Templates for van Westendorp PSM for
  Lighthouse Studio and Excel". Online available at
  \url{https://sawtoothsoftware.com/resources/software-downloads/tools/van-westendorp-price-sensitivity-meter}
}

\examples{

# assuming a skewed sample with only 1/3 women and 2/3 men

input_data <- data.frame(tch = round(rnorm(n = 250, mean = 8, sd = 1.5), digits = 2),
                         ch = round(rnorm(n = 250, mean = 12, sd = 2), digits = 2),
                         ex = round(rnorm(n = 250, mean = 13, sd = 1), digits = 2),
                         tex = round(rnorm(n = 250, mean = 15, sd = 1), digits = 2),
                         gender = sample(x = c("male", "female"),
                                         size = 250,
                                         replace = TRUE,
                                         prob = c(2/3, 1/3)))

# ... and in which women have on average 1.5x the price acceptance of men
input_data$tch[input_data$gender == "female"] <- input_data$tch[input_data$gender == "female"] * 1.5
input_data$ch[input_data$gender == "female"] <- input_data$ch[input_data$gender == "female"] * 1.5
input_data$ex[input_data$gender == "female"] <- input_data$ex[input_data$gender == "female"] * 1.5
input_data$tex[input_data$gender == "female"] <- input_data$tex[input_data$gender == "female"] * 1.5

# creating a sample design object using the survey package
# ... assuming that gender is balanced equally in the population of 10000

input_data$gender_pop <- 5000

input_design <- survey::svydesign(ids = ~ 1, # no clusters
                          probs = NULL, # hence no cluster samling probabilities,
                          strata = input_data$gender, # stratified by gender
                          fpc = input_data$gender_pop, # strata size in the population
                          data = input_data)
                          # data object used as input: no need to specify single variables

output_weighted_psm <- psm_analysis_weighted(toocheap = "tch",
  cheap = "ch",
  expensive = "ex",
  tooexpensive = "tex",
  design = input_design)

summary(output_weighted_psm)
}