Moment Conditions for Stochastic Discount Factor (SDF) Following extract has been taken from Computing Generalized Method of Moments and Generalized Empirical Likelihood with R, the Vignette of gmm R package. 
In some cases the theory is directly based on moment conditions. When
it is the case, testing the validity of these conditions becomes a
way of testing the theory. Jagannathan et al. (2002) present
several GMM applications in finance and one of them is the stochastic
discount factor (SDF) representation of the CAPM. The general theory
implies that $\mathbb{E}\left(m_{t}R_{it}\right)=1$ for all $i$,
where $m_{t}$ is the SDF and $R_{it}$ is the gross return $\left(1+r_{it}\right)$.
It can be shown that if the CAPM holds, $m_{t}=\theta_{0}+\theta_{1}R_{mt}$
which implies the following moment conditions: 
$$
\mathbb{E}\left[R_{it}\left(\theta_{0}-\theta_{1}R_{mt}\right)-1\right]=0~~\mbox{for}~~i=1,...,N.
$$
which can be tested as follows:
g5 <- function(tet, x) {
gmat <- (tet[1] + tet[2] * (1 + c(x[, 1]))) * (1 + x[, 2:6]) -
return(gmat)
}

I wonder how the moment conditions are obtained and how they are coded in R. Any help will be highly appreciated. Thanks in advance for your help.
 A: How these moment conditions result from CAPM and standard SDF equations
Consider a gross return on some investment, $R_{i,t}$. The equation:
$$
E[m_tR_{i,t}]=1
$$
is one of the more fundamental equations of asset pricing. Here $m_{t}$ is called a stochastic discount factor. The theory behind an SDF is a little outside the purview of CrossValidated, if you want to learn more about it I'd suggest checking out John Cochrane's Coursera course on asset pricing, or his textbook. You could also ask about SDF's on the economics stackexchange site. 
Imagine a risk free return, $R_{f,t}$, and let's look at excess returns over the risk free rate $r_{i,t}$. Then:
$$
E[m_tr_{i,t}]=E[m_tR_{i,t}]-E[m_tR_{f,t}]=1-1=0
$$
This equation can be decomposed in a different matter:
$$
E[m_tr_{i,t}]=E[m_t]E[r_{i,t}]+\text{cov}(m_t,r_{i,t})
$$
taking these last two equations together and remembering that for a regression of $r_{i,t}$ on $m_t$, the coefficient $\beta_i = \text{cov}(m_t,r_{i,t})/\text{var}(m_t)$ we have:
$$
E[r_{i,t}]=\beta_i \dfrac{\text{var}(m_t)}{E[m_t]}
$$
which follows from the definition of covariance. 
The version of the CAPM you may be used to is:
$$
E[r_{i,t}]=\beta_i E[r_{m,t}]
$$
where $r_{m,t}$ is the return on the market. We can start to see connections between this and the SDF by looking at the two equations above. Let's say that $m_t=a-br_{m,t}$ will this satisfy the above? If the SDF is a linear function of $r_{m,t}$, then all $\beta$'s can be determined (up to a constant scale) by regressions of $r_{i,t}$ on $r_{m,t}$, as in this case:
$$
\text{cov}(r_{i,t},m_t)= -b\text{cov}(r_{m,t},r_{i,t})
$$
We are then left with two requirements for $b$ and $a$. First, the market return itself must be an asset with $\beta$ of 1, so that:
$$
E[r_{m,t}]=\dfrac{\text{var}(m_t)}{E[m_t]}
$$
Second,
$$
E[R_{f,t}]=\dfrac{1}{E[m_t]}
$$
this leaves us two equations in two unknowns, $a$ and $b$ so we know this SDF representation of the CAPM model will work.
Finally, since the risk free rate is a constant we can rewrite our SDF as $m_t=\theta_0-\theta_1R_{m,t}$. Substitute our new $m_t$ into the first equation and you have the equation presented in the question:
$$
E[m_tR_{i,t}]=1\Rightarrow E[(\theta_0-\theta_1R_{m,t})R_{i,t}]=1\Rightarrow E[(\theta_0-\theta_1R_{m,t})R_{i,t}-1]=0
$$
This is a moment condition: a function of the data which in expectation must be equal to zero. If the CAPM model is correct, this equation must hold for every asset in the economy.
How to program these conditions into R
Unfortunately, we do not know the expected market premium or the risk free rate, so we have to estimate $\theta_0$ and $\theta_1$ from the data (even if we did know these quantities this might still be interesting as a test of the model).
The GMM code in this package needs you to provide a function which will output the data in a form that gives the moment equation for each observation, so it can then take the expectation over your data. In otherwords, it needs a number for each observation, $e_{i,t}$:
$$
e_{i,t}=(\theta_0-\theta_1R_{m,t})R_{i,t}-1
$$
this particular setup seems to be for five test assets (five different $i$'s). 
GMM will choose $\theta_0$ and $\theta_1$ such that a weighted combination of these expectational errors are minimized, and the moment conditions hold as close as possible.
The code you are given is:
g5 <- function(tet, x) {
gmat <- (tet[1] + tet[2] * (1 + c(x[, 1]))) * (1 + x[, 2:6]) -
return(gmat)
}

this implies a matrix of net returns, x, where x[,1] is the market return, and x[,2:6] are five returns on different assets. Since they are net returns they have to be converted to gross returns by adding 1 to each of them. The rest follows from the moment condition above. The output of this function will be a $T\times 5$ matrix, where $T$ is the number of time periods, and each element $e_{i,t}$ is an expectational error as above. If you took the average over these columns, you would have an (unweighted) version of the five moment conditions. 
Meanwhile tet will be a 2 entry vector, containing parameter estimates, $[\theta_0,\theta_1]$. The gmm package in R then can estimate these parameters with the command:
est <- gmm(g5, x = x, t0 = rep(0, 2))

and on output you will have estimates for $\theta_0$ and $\theta_1$ as well as information about model fit.
