data(twinstut)
twinstut0 <- subset(twinstut, tvparnr<4000)
twinstut <- twinstut0
twinstut$binstut <- (twinstut$stutter=="yes")*1
theta.des <- model.matrix( ~-1+factor(zyg),data=twinstut)
margbin <- glm(binstut~factor(sex)+age,data=twinstut,family=binomial())
bin <- binomial.twostage(margbin,data=twinstut,var.link=1,
clusters=twinstut$tvparnr,theta.des=theta.des,detail=0,
method="nr")
summary(bin)
lava::estimate(coef=bin$theta,vcov=bin$var.theta,f=function(p) exp(p))
twinstut$cage <- scale(twinstut$age)
theta.des <- model.matrix( ~-1+factor(zyg)+cage,data=twinstut)
bina <- binomial.twostage(margbin,data=twinstut,var.link=1,
clusters=twinstut$tvparnr,theta.des=theta.des,detail=0)
summary(bina)
theta.des <- model.matrix( ~-1+factor(zyg)+factor(zyg)*cage,data=twinstut)
bina <- binomial.twostage(margbin,data=twinstut,var.link=1,
clusters=twinstut$tvparnr,theta.des=theta.des)
summary(bina)
out <- easy.binomial.twostage(stutter~factor(sex)+age,data=twinstut,
response="binstut",id="tvparnr",var.link=1,
theta.formula=~-1+factor(zyg1))
summary(out)
## refers to zygosity of first subject in eash pair : zyg1
## could also use zyg2 (since zyg2=zyg1 within twinpair's))
## do not run t save time
# desfs <- function(x,num1="zyg1",namesdes=c("mz","dz","os"))
# c(x[num1]=="mz",x[num1]=="dz",x[num1]=="os")*1
#
#out3 <- easy.binomial.twostage(binstut~factor(sex)+age,
# data=twinstut, response="binstut",id="tvparnr",
# var.link=1,theta.formula=desfs,
# desnames=c("mz","dz","os"))
#summary(out3)
## Reduce Ex.Timings
n <- 1000
set.seed(100)
dd <- simBinFam(n,beta=0.3)
binfam <- fast.reshape(dd,varying=c("age","x","y"))
## mother, father, children (ordered)
head(binfam)
########### ########### ########### ########### ########### ###########
#### simple analyses of binomial family data
########### ########### ########### ########### ########### ###########
desfs <- function(x,num1="num1",num2="num2")
{
pp <- 1*(((x[num1]=="m")*(x[num2]=="f"))|(x[num1]=="f")*(x[num2]=="m"))
pc <- (x[num1]=="m" | x[num1]=="f")*(x[num2]=="b1" | x[num2]=="b2")*1
cc <- (x[num1]=="b1")*(x[num2]=="b1" | x[num2]=="b2")*1
c(pp,pc,cc)
}
ud <- easy.binomial.twostage(y~+1,data=binfam,
response="y",id="id",
theta.formula=desfs,desnames=c("pp","pc","cc"))
summary(ud)
udx <- easy.binomial.twostage(y~+x,data=binfam,
response="y",id="id",
theta.formula=desfs,desnames=c("pp","pc","cc"))
summary(udx)
########### ########### ########### ########### ########### ###########
#### now allowing parent child POR to be different for mother and father
########### ########### ########### ########### ########### ###########
desfsi <- function(x,num1="num1",num2="num2")
{
pp <- (x[num1]=="m")*(x[num2]=="f")*1
mc <- (x[num1]=="m")*(x[num2]=="b1" | x[num2]=="b2")*1
fc <- (x[num1]=="f")*(x[num2]=="b1" | x[num2]=="b2")*1
cc <- (x[num1]=="b1")*(x[num2]=="b1" | x[num2]=="b2")*1
c(pp,mc,fc,cc)
}
udi <- easy.binomial.twostage(y~+1,data=binfam,
response="y",id="id",
theta.formula=desfsi,desnames=c("pp","mother-child","father-child","cc"))
summary(udi)
##now looking to see if interactions with age or age influences marginal models
##converting factors to numeric to make all involved covariates numeric
##to use desfai2 rather then desfai that works on binfam
nbinfam <- binfam
nbinfam$num <- as.numeric(binfam$num)
head(nbinfam)
desfsai <- function(x,num1="num1",num2="num2")
{
pp <- (x[num1]=="m")*(x[num2]=="f")*1
### av age for pp=1 i.e parent pairs
agepp <- ((as.numeric(x["age1"])+as.numeric(x["age2"]))/2-30)*pp
mc <- (x[num1]=="m")*(x[num2]=="b1" | x[num2]=="b2")*1
fc <- (x[num1]=="f")*(x[num2]=="b1" | x[num2]=="b2")*1
cc <- (x[num1]=="b1")*(x[num2]=="b1" | x[num2]=="b2")*1
agecc <- ((as.numeric(x["age1"])+as.numeric(x["age2"]))/2-12)*cc
c(pp,agepp,mc,fc,cc,agecc)
}
desfsai2 <- function(x,num1="num1",num2="num2")
{
pp <- (x[num1]==1)*(x[num2]==2)*1
agepp <- (((x["age1"]+x["age2"]))/2-30)*pp ### av age for pp=1 i.e parent pairs
mc <- (x[num1]==1)*(x[num2]==3 | x[num2]==4)*1
fc <- (x[num1]==2)*(x[num2]==3 | x[num2]==4)*1
cc <- (x[num1]==3)*(x[num2]==3 | x[num2]==4)*1
agecc <- ((x["age1"]+x["age2"])/2-12)*cc ### av age for children
c(pp,agepp,mc,fc,cc,agecc)
}
udxai2 <- easy.binomial.twostage(y~+x+age,data=binfam,
response="y",id="id",
theta.formula=desfsai,
desnames=c("pp","pp-age","mother-child","father-child","cc","cc-age"))
summary(udxai2)
Run the code above in your browser using DataLab