I am new to R, and I don\'t understand yet completely the logic of its calculations...
I cannot overcome my problem with the help from previous posts either.
Ok, seems like I have kind of solution with the help of:
http://www.talkstats.com/showthread.php/44314-calculate-marginal-effects-using-mlogit-package
and
How can I view the source code for a function?
apparently it was enough to see for the:
> getAnywhere(effects.mlogit)
A single object matching ‘effects.mlogit’ was found
It was found in the following places
registered S3 method for effects from namespace mlogit
namespace:mlogit
with value
function (object, covariate = NULL, type = c("aa", "ar", "rr",
"ra"), data = NULL, ...)
{
type <- match.arg(type)
if (is.null(data)) {
P <- predict(object, returnData = TRUE)
data <- attr(P, "data")
attr(P, "data") <- NULL
}
else P <- predict(object, data)
newdata <- data
J <- length(P)
alt.levels <- names(P)
pVar <- substr(type, 1, 1)
xVar <- substr(type, 2, 2)
cov.list <- lapply(attr(formula(object), "rhs"), as.character)
rhs <- sapply(cov.list, function(x) length(na.omit(match(x,
covariate))) > 0)
rhs <- (1:length(cov.list))[rhs]
eps <- 1e-05
if (rhs %in% c(1, 3)) {
if (rhs == 3) {
theCoef <- paste(alt.levels, covariate, sep = ":")
theCoef <- coef(object)[theCoef]
}
else theCoef <- coef(object)[covariate]
me <- c()
for (l in 1:J) {
newdata[l, covariate] <- data[l, covariate] + eps
newP <- predict(object, newdata)
me <- rbind(me, (newP - P)/eps)
newdata <- data
}
if (pVar == "r")
me <- t(t(me)/P)
if (xVar == "r")
me <- me * matrix(rep(data[[covariate]], J), J)
dimnames(me) <- list(alt.levels, alt.levels)
}
if (rhs == 2) {
newdata[, covariate] <- data[, covariate] + eps
newP <- predict(object, newdata)
me <- (newP - P)/eps
if (pVar == "r")
me <- me/P
if (xVar == "r")
me <- me * data[[covariate]]
names(me) <- alt.levels
}
me
}
<environment: namespace:mlogit>
Then to copy the function and modyfy its 16 line:
myeffects<-function (object, covariate = NULL, type = c("aa", "ar", "rr",
"ra"), data = NULL, ...)
{
type <- match.arg(type)
if (is.null(data)) {
P <- predict(object, returnData = TRUE)
data <- attr(P, "data")
attr(P, "data") <- NULL
}
else P <- predict(object, data)
newdata <- data
J <- length(P)
alt.levels <- names(P)
pVar <- substr(type, 1, 1)
xVar <- substr(type, 2, 2)
cov.list <- strsplit(as.character(attr(formula(object), "rhs")), " + ", fixed = TRUE)
rhs <- sapply(cov.list, function(x) length(na.omit(match(x,
covariate))) > 0)
rhs <- (1:length(cov.list))[rhs]
eps <- 1e-05
if (rhs %in% c(1, 3)) {
if (rhs == 3) {
theCoef <- paste(alt.levels, covariate, sep = ":")
theCoef <- coef(object)[theCoef]
}
else theCoef <- coef(object)[covariate]
me <- c()
for (l in 1:J) {
newdata[l, covariate] <- data[l, covariate] + eps
newP <- predict(object, newdata)
me <- rbind(me, (newP - P)/eps)
newdata <- data
}
if (pVar == "r")
me <- t(t(me)/P)
if (xVar == "r")
me <- me * matrix(rep(data[[covariate]], J), J)
dimnames(me) <- list(alt.levels, alt.levels)
}
if (rhs == 2) {
newdata[, covariate] <- data[, covariate] + eps
newP <- predict(object, newdata)
me <- (newP - P)/eps
if (pVar == "r")
me <- me/P
if (xVar == "r")
me <- me * data[[covariate]]
names(me) <- alt.levels
}
me
}
Now the results are as of:
> myeffects(mlogit.data2,covariate="RSVS",data=zz)
2 0 1 4 5 6
3.612318e-03 5.368693e-04 -4.903995e-08 -5.382731e-03 1.238768e-03 -5.175053e-06
You could use colMeans
op <- options(scipen= 100, digits=2)
colMeans(mldata[,3:11], na.rm=TRUE)
# CHINN DEBT ERA INFL MONEY
# -0.27 115.25 0.20 33.66 74.66
#OPENNESS RESERVES RGDP RSVS
# 92.06 18809465124.14 136.56 18.81
options(op)
or summarise_each
from dplyr
library(dplyr)
mldata %>%
summarise_each(funs(round(mean(., na.rm=TRUE),2)), CHINN:RSVS)
# CHINN DEBT ERA INFL MONEY OPENNESS RESERVES RGDP RSVS
#1 -0.27 115.25 0.2 33.66 74.66 92.06 18809465124 136.56 18.81