Skip to content

Instantly share code, notes, and snippets.

@naomispence
Created May 8, 2017 19:00
Show Gist options
  • Save naomispence/f2f8010769aecb319846026c24add7e1 to your computer and use it in GitHub Desktop.
Save naomispence/f2f8010769aecb319846026c24add7e1 to your computer and use it in GitHub Desktop.
#Look at the mean of an interval variable
mean(GSS$childs, na.rm=TRUE)
# This is a generalized linear model of an interval variable with just an intercept, no independent variable
regchilds<-glm(GSS$childs~1, data=GSS)
summary(regchilds)
# This is a generalized linear model of an interval variable with an intercept and one interval-ratio independent variable
regchilds2<-glm(GSS$childs~age, data=GSS)
summary(regchilds2)
# This is a generalized linear model of an interval variable with an intercept and one dichotomous independent variable
regchilds3<-glm(GSS$childs~sex, data=GSS)
summary(regchilds3)
#create a dichotomous variable coded 0, 1 (or TRUE, FALSE)
GSS$nochild <-as.numeric(GSS$childs) == 0
#Look at the proportion of 1 (TRUE) values of the dichotomous variable coded 0, 1
#you could run the mean of the 0,1 variable to get the proportion or you could run the crosstab to get percent and divide by 100
mean(GSS$nochild)
crosstab(GSS, row.vars = "nochild")
#see what happens if you ask R to give you the "logit" of the proportion of 1 (TRUE) values
#to do so, type the proportion in parentheses in the statement below
logit(.271)
# This is a generalized linear model a dichotomous variable with just an intercept, no independent variable
regnochild<-glm(GSS$nochild~1, data=GSS, family = binomial(link = logit))
summary(regnochild)
# This is a generalized linear model a dichotomous variable with an intercept and one interval-ratio independent variable
regnochild2<-glm(GSS$nochild~age, data=GSS, family = binomial(link = logit))
summary(regnochild2)
# This is a generalized linear model a dichotomous variable with an intercept and one dichotomous independent variable
regnochild3<-glm(GSS$nochild~sex, data=GSS, family = binomial(link = logit))
summary(regnochild3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment