---
title: "Pubpeer analysis"
author: "Dorothy Bishop"
date: "2025-05-10"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(tidyverse)

require(xlsx)
```

Hand coding is done on ppdf1_coded.csv.  This comes from ppdf1_checkedb.xlsx, which has all rows manually coded in the column newcat.

```{r readcoded}
mydf <- read.csv('ppdf1_coded.csv')
names(mydf)[c(20,23)]<-c('au_response','inst_involvement')
categories <- c('Minor','Critique','Data_error','Data_error_questionable','Protocol_error','Ethics_breach','COI','Authorship','Plagiarism','Papermill','Fabrication_falsification','Other')

#cross tabulation of uni by category
myt<-table(mydf$uni,mydf$newcat)
myt<-as.data.frame.array(myt)
names(myt)<-c('Minor','Critique','Data_error','Data_error_qu','Protocol','Ethics','COI','Authorship','Plagiarism','Papermill','Fabrication','Other')

mysumtab<-as.data.frame.array(table(mydf$newcat))
mysumtab<-cbind(categories, mysumtab)
names(mysumtab)[2] <-'N'

write.xlsx(mysumtab,'mysumtab.xlsx')

# filter to keep just those with 100% - i.e. main author at UK uni 

mydf100<-mydf[mydf$inst_involvement==100,]
#now redo creation of mysumtab
mysumtabf<-as.data.frame.array(table(mydf100$newcat))

mytf<-table(mydf100$uni,mydf100$newcat) #filtered table with only those 100%
mytf<-as.data.frame.array(mytf)

mysumtabf$unis_2_or_more <-0
mysumtabf$unis_1_or_more <-0  #added for completeness (original focus just on 2 or more)

for (i in 1:nrow(mysumtabf)){
  mysumtabf$unis_2_or_more[i] <- length(which(mytf[,i]>1))
  mysumtabf$unis_1_or_more[i] <- length(which(mytf[,i]>0))
}

mysumtab<-cbind(mysumtab,mysumtabf)
names(mysumtab)[3:5]<-c('N_from_uni100','N_uni100_with_2plus','N_uni100_with_1plus')

#Now find the unis with 2+ for category 6, 10 and 11 - still using filtered by 100%

#Make dataframe to hold results
unis<-data.frame(matrix(data=NA,nrow=sum(mysumtab$N_uni100_with_2plus[4:11]),ncol=4))
names(unis)<-c("Unis","Category","N","Notes")
thisrow<-0
for (i in c(4,10,11)){
  w<-which(mytf[,i]>1)
  rowrange<-(thisrow+1):(thisrow+length(w))
  unis$Unis[rowrange]<-rownames(mytf)[w]
  unis$Category[rowrange]<-categories[i]
  unis$N[rowrange]<-mytf[w,i]
  thisrow<-max(rowrange)
  
}

unis<-unis[order(unis$Unis),]

#Repeat for unis with 1+ for categories 4:11
#This is for supplementary table
unislax<-data.frame(matrix(data=NA,nrow=sum(mysumtab$N_uni100_with_1plus[4:11]),ncol=4))
names(unislax)<-c("Unis","Category","N","Notes")
thisrow<-0
for (i in 4:11){
  w<-which(mytf[,i]>0)
  rowrange<-(thisrow+1):(thisrow+length(w))
  unislax$Unis[rowrange]<-rownames(mytf)[w]
  unislax$Category[rowrange]<-categories[i]
  unislax$N[rowrange]<-mytf[w,i]
  thisrow<-max(rowrange)
  
}

unislax<-unislax[order(unislax$Unis),]
write.csv(unislax,'all_cases_4_11_Uni100.csv',row.names=F)
length(unique(unislax$Unis)) #N unis with at least one comments coded 4-11

#get proportions for different categories in col 3

mysumtab$p_Nfromuni<-mysumtab[,3]/sum(mysumtab[,3])
```
To drill down to individual cases, filter main df by institution%, category (4,10,11).


```{r ForTable7}
focusdf<-mydf100[mydf100$uni %in% unique(unis$Unis),]
focusdf<-focusdf[focusdf$newcat %in% c(4,10,11),]

focusdf<-focusdf[order(focusdf$uni,focusdf$newcat),]
#This file includes single cases, not just 2+, so need to refine further


focust<-table(focusdf$uni,focusdf$newcat) #this shows some cases where a uni has just one case in that category

#we remove those by stepping through each row of focusdf and resetting a counter when the category or uni changes . If the 'thiscounter' variable is only 1 at that point then the prior row has a marker added so that we can then delete it later.

counter <- 0
focusdf$marker <-0
for (i in 2:nrow(focusdf)){
   counter <- counter + 1
   thiscounter <- counter
   if(focusdf$uni[i]!=focusdf$uni[(i-1)]){counter <- 0}
   if(focusdf$newcat[i]!=focusdf$newcat[(i-1)]){counter <- 0}
   if((counter==0) && (thiscounter ==1))  {focusdf$marker[(i-1)] <- 1}
   thiscounter <-0
}


w<-which(focusdf$marker==1)
focusdf<-focusdf[-w,]

write.xlsx(focusdf,'focusdf.xlsx') #This is data frame containing individual PubPeer rows for Table7 cases. 

```



```{r focuspplax}
#This is file with individual rows for all unis with 1+ codes from 4:11
focusdf2<-mydf100[mydf100$uni %in% unique(unislax$Unis),]
focusdf2<-focusdf2[focusdf2$newcat %in% 4:11,]

focusd2f<-focusdf2[order(focusdf2$uni,focusdf2$newcat),]
 #This is a superset of focusdf.

write.xlsx(focusdf2,'focusdf_1plus.xls')

```


