R Program for regression of proportion is a single conginuous program. To make it easier to follow, the listing is divided into 2 sections

**Section 1: Initial data input and matrix of summaries**

# Section 1: Preparation
dat = ("
X NPos NNeg
1990 10 100
1992 8 50
1995 61 300
")
df <- read.table(textConnection(dat),header=TRUE) # conversion to data frame
df$RowTot <- df$NPos + df$NNeg # total number each row
df$Prob <- df$NPos / df$RowTot # probability of Pos each row
df # Summary of Input Data

The initial matrix with all the data necessary for calculations are as follows. Please note:

- X is the predictor in the regression formular, and should be at least ordinal and preferably intrval mesurements. In this example it represents the year the data is collected
- NPos is the number of cases with positive attributes
- NNeg is the number of cases with negative attributes
- RowTot is NPos + NNeg for the row
- Prob is probability, NPos / RowTot

> df # Summary of Input Data
X NPos NNeg RowTot Prob
1 1990 10 100 110 0.09090909
2 1992 8 50 58 0.13793103
3 1995 61 300 361 0.16897507

**Section 2 is the actual calculations**
# Preparation for calculation
rows = nrow(df)
posTot = sum(df$NPos)
negTot = sum(df$NNeg)
tot = sum(df$RowTot)
# vevtors for results
Source <- vector()
ChiSq <- vector()
DF <- vector()
P<- vector()
# calculate total chi sq
zw = 0
chiTot = 0
dfTot = rows - 1
for(i in 1:rows) # for each row
{
zw = zw + df$X[i] * df$RowTot[i] # row value x row count
e = df$RowTot[i] * posTot / tot; # expected
o = df$NPos[i] # observed number pos
chiTot = chiTot + (o - e)**2 / e # add to Chi Sq
e = df$RowTot[i] * negTot / tot; # expected
o = df$NNeg[i] # observed number beg
chiTot = chiTot + (o - e)**2 / e # add to Chi Sq
}
pTot = 1 - pchisq(chiTot, df=dfTot)
Source <- append(Source, "Total") # add to vectors for eventual display
ChiSq <- append(ChiSq, chiTot)
DF <- append(DF, dfTot)
P<- append(P,pTot)
#c(chiTot,pTot)
# Calculate regression and its chi sq
p2 = posTot / tot; # probability of col 1
top = 0;
bot = 0;
for(i in 1:rows)
{
top = top + df$X[i] * df$NPos[i] # sum row value x col 1
bot = bot + df$X[i]^2 * df$RowTot[i] # row val sq x row count
}
#Calculation of regression coefficient
top = top - posTot * zw / tot
bot = bot - zw^2 / tot
reg = top / bot # regression coefficient
# calculate chi sq regression
chiReg = top^2 / (bot * p2 * (1 - p2)) # chi sq regression
pReg = 1 - pchisq(chiReg, df=1)
Source <- append(Source, "Regression") # add to vectors for eventual display
ChiSq <- append(ChiSq, chiReg)
DF <- append(DF, 1)
P<- append(P,pReg)
# Calculate residual chi sq
chiRes = chiTot - chiReg # chi sq residual
dfRes = dfTot - 1
pRes = 1 - pchisq(chiRes, df=dfRes)
Source <- append(Source, "Residual") # add to vectors for eventual display
ChiSq <- append(ChiSq, chiRes)
DF <- append(DF, dfRes)
P<- append(P,pRes)
# output
dfRes <- data.frame(Source, ChiSq, DF, P) # combine vectors into data frame for display
dfRes # display chi sq, df, and significance in p
# Regression coefficient
reg #regression coefficient changes in propbabilit per unit of X

The results are as follows

> dfRes
Source ChiSq DF P
1 Total 4.11131566 2 0.12800860
2 Regression 4.01760141 1 0.04502771
3 Residual 0.09371425 1 0.75950732
> reg #regression coefficient changes in propbability per unit of X
[1] 0.014958