#
# s41598-022-fishing.R, 24 May 26
#
# Data from:
#
# On the round number bias and wisdom of crowds in different response formats for numerical estimation
# Hidehito Honda and Rina Kagawa and Masaru Shirasuna

library("colorspace")
library("plyr")
library("reshape2")


par(bty="l")
par(las=1)
par(pch="+")
pal_col=rainbow(2)


count_ans=function(quest, col_str)
{
print(mean(answer))
t=count(round(quest/5)*5)
t$freq=100*t$freq/length(quest)
lines(t, type="h", col=col_str)
}


ex1=read.csv("Experiment1.csv")
ans=read.csv("questval.csv")


# Move the column names into a column
lex1=melt(ex1, measure.vars=c(paste0("Q", 1:9)), variable.name="Question")
lex1$Question=as.character(lex1$Question)

QA=merge(lex1, ans, by="Question")

QA$anchored=(QA$Group %in% paste0("Scale ", c(1, 3, 5)))
QA$isnumber=(QA$Group == "Number")

# Various ways of measuring the erro rin the answer
QA$ans_dist=abs(QA$Correct-QA$value)
QA$ans_err=QA$ans_dist/QA$Correct
QA$rnd_dist=abs(QA$Rndval-QA$value)

# Fit the absolute distance
# dot_mod=glm(ans_dist ~ log(Correct)+Group, data=QA)
# dot_mod=glm(ans_dist ~ log(Correct)+isnumber+anchored, data=QA)
# dot_mod=glm(ans_dist ~ log(Correct)+log(Correct):isnumber, data=QA)
# dot_mod=glm(sqrt(ans_dist) ~ Correct+isnumber, data=QA,
dot_mod=glm(ans_dist ~ log(Correct)+isnumber, data=QA,
				subset=(Question != "Q9"))
summary(dot_mod)


# For the relative distance
err_mod=glm(ans_err ~ log(Correct)+isnumber, data=QA,
				subset=(Question != "Q9"))
summary(err_mod)


# log(ans$Correct)*6.77
# [1] 35.26822 38.31469 39.84892 41.40459 42.76720 43.66968 44.53212 45.31380
# [9] 46.67005

# Plot density of the answers
plot(density(subset(ex1, Group == "Number")$Q1, from=30),  col=pal_col[1],
	xlim=c(0, 1300),
	xlab="Estimate", ylab="Density")
lines(density(subset(ex1, Group != "Number")$Q1, from=30), col=pal_col[2])

lines(density(subset(ex1, Group == "Number")$Q5, from=30), col=pal_col[1])
lines(density(subset(ex1, Group != "Number")$Q5, from=30), col=pal_col[2])

lines(density(subset(ex1, Group == "Number")$Q9, from=30), col=pal_col[1])
lines(density(subset(ex1, Group != "Number")$Q9, from=30), col=pal_col[2])


# Plot number of occurrences
plot(0, type="n",
	xaxs="i", yaxs="i",
	xlim=c(0, 1000), ylim=c(0, 25),
	xlab="Estimate", ylab="Occurrences")
count_ans(subset(ex1, Group == "Number")$Q1, pal_col[1])
count_ans(subset(ex1, Group != "Number")$Q1, pal_col[2])
lines(c(183, 183), c(0, 25), col="grey")

legend("topright", legend=c("Language", "Scale"), cex=1.2,
                        fill=pal_col, border="white", bty="n")

dev.copy(dev=png, file="est-lang-scale-183.png")
dev.off()


plot(0, type="n",
	xaxs="i", yaxs="i",
	xlim=c(0, 1000), ylim=c(0, 25),
	xlab="Estimate", ylab="Occurrences")
count_ans(subset(ex1, Group == "Number")$Q5, pal_col[1])
count_ans(subset(ex1, Group != "Number")$Q5, pal_col[2])

count_ans(subset(ex1, Group == "Number")$Q9, pal_col[1])
count_ans(subset(ex1, Group != "Number")$Q9, pal_col[2])

