Last week was the exception in that I did not provide course materials in R Markdown…today that changes
R Markdown is a form of the Markdown language (https://en.wikipedia.org/wiki/Markdown)
Markdown makes writing very simple:
When you do need a type of document, you can then use any number of programs to make it look nice:
R Markdown allows you to embed R script within the document, providing syntax snippets and output directly to your final document format
You can find lots of helpful tips on R Markdown on some of these sites:
irtItemProb = function(a, b, c=0, theta){
prob = c + (1-c) * exp(a*(theta-b))/(1+exp(a*(theta-b)))
return(prob)
}
trueTheta = 0
nItems = 5
nItems = 5
bRange = c(-2,2)
aRange = c(1,2)
bSE = 1
aSE = 1
nSamples = 1000
# draw mean values of a, b
a = runif(n = nItems, min = aRange[1], max = aRange[2])
b = runif(n = nItems, min = bRange[1], max = bRange[2])
# draw items
itemResponses = rbinom(n = nItems, size = 1, prob = irtItemProb(a = a, b = b, theta = 1))
thetaChain = list(rep(NA, nSamples), rep(NA, nSamples))
# initialize theta values
curTheta = trueTheta
curThetaRand = trueTheta
for (iteration in 1:nSamples){
# draw item parameters (if random)
iterA = rnorm(n = nItems, mean = a, sd = aSE)
iterB = rnorm(n = nItems, mean = b, sd = bSE)
# calculate current likelihood of the data | theta
curLogLike = sum(dbinom(x = itemResponses, size = 1, prob = irtItemProb(a = a, b = b, theta = curTheta), log = TRUE))
curLogLikeRand = sum(dbinom(x = itemResponses, size = 1, prob = irtItemProb(a = iterA, b = iterB, theta = curThetaRand), log = TRUE))
# draw new theta value
propTheta = rnorm(n = 1, mean = curTheta, sd = 1)
propThetaRand = rnorm(n = 1, mean = curThetaRand, sd = 1)
# calculate proposed likelihood of the data | theta
propLogLike = sum(dbinom(x = itemResponses, size = 1, prob = irtItemProb(a = a, b = b, theta = propTheta), log = TRUE))
propLogLikeRand = sum(dbinom(x = itemResponses, size = 1, prob = irtItemProb(a = iterA, b = iterB, theta = propThetaRand), log = TRUE))
# do MH:
if (log(runif(n = 1)) < (propLogLike-curLogLike)){
# accept
curTheta = propTheta
}
# do MH:
if (log(runif(n = 1)) < (propLogLikeRand-curLogLikeRand)){
# accept
curThetaRand = propThetaRand
}
thetaChain[[1]][iteration] = curTheta
thetaChain[[2]][iteration] = curThetaRand
}
par(mfrow = c(1,2))
plot(thetaChain[[1]], type="l", ylab = expression(theta), xlab = "Iteration Number")
lines(thetaChain[[2]], type="l", col = 2)
plot(density(thetaChain[[1]]), col = 1, main="")
lines(density(thetaChain[[2]]), col = 2)
par(mfrow = c(3,2))
plot(thetaChain[[1]], type="l", ylab = expression(theta), xlab = "Iteration Number")
plot(thetaChain[[2]], type="l", ylab = expression(theta), xlab = "Iteration Number", col =2)
plot(density(thetaChain[[1]]), col = 1, main="")
plot(density(thetaChain[[2]]), col = 2, main="")
plot(thetaChain[[1]], type="l", ylab = expression(theta), xlab = "Iteration Number")
lines(thetaChain[[2]], type="l", col = 2)
plot(density(thetaChain[[1]]), col = 1, main="")
lines(density(thetaChain[[2]]), col = 2)
To compile the whole document (called “Knitting” as it uses a package named knitr), press the Knit button or use the keystroke command-shift-K
To run a chunk of R code, find and press the button on the top right of the chunk
Note: If chunks later in the document depend on chunks at the beginning, you will have to run the beginning ones first (chunks use the current Global R Environment for variables and functions)