glm

update() a model inside a function with local covariate

断了今生、忘了曾经 提交于 2019-12-23 07:56:20
问题 I need to update a regression model from inside a function. Ideally, the function should work with any kind of models ( lm , glm , multinom , clm ). More precisely, I need to add one or several covariates that are defined inside the function. Here is an exemple. MyUpdate <- function(model){ randData <- data.frame(var1=rnorm(length(model$residuals))) model2 <- update(model, ".~.+randData$var1") return(model2) } Here is an example use data(iris) model1 <- lm(Sepal.Length~Species, data=iris)

Factor/level error in mixed model

无人久伴 提交于 2019-12-23 04:52:26
问题 I am running a mixed model on something akin to this data: df<-data.frame(stage=c("a","a","a","a","b","b","b","b","c","c","c","c"), nematode=c("fn","fn","bn","bn","fn","fn","bn","bn","fn","fn","bn","bn"), id2=c(1,2,3,4,1,2,3,4,1,2,3,4), value=c(1,0,0,2,3,1,1,2,0,0,0,2)) The model I am trying to fit is: stage.id <- function(x) round(summary(glmer(value ~ stage + (1 | id2),family="poisson", data = x))$coefficients[2, c(1, 2, 4)], 3) models.id0 <- ddply(tree2, .(stage, nematode), stage.id)

How to set contrasts for my variable in regression analysis with R?

狂风中的少年 提交于 2019-12-22 19:51:12
问题 During coding, I need to change the dummy value assigned to a factor. However, the following code does not work. Any suggestion? test_mx= data.frame(a= c(T,T,T,F,F,F), b= c(1,1,1,0,0,0)) test_mx a b 1 TRUE 1 2 TRUE 1 3 TRUE 1 4 FALSE 0 5 FALSE 0 6 FALSE 0 model= glm(b ~ a, data= test_mx, family= "binomial") summary(model) model= glm(a ~ b, data= test_mx, family= "binomial") summary(model) Here I will get the coef for b is 47. Now if I swap the dummy value, it should be -47 then. However, this

Why the auc is so different from logistic regression of sklearn and R

…衆ロ難τιáo~ 提交于 2019-12-22 09:47:39
问题 I use a same dataset to train logistic regression model both in R and python sklearn. The dataset is unbalanced. And I find that the auc is quite different. This is the code of python: model_logistic = linear_model.LogisticRegression() #auc 0.623 model_logistic.fit(train_x, train_y) pred_logistic = model_logistic.predict(test_x) #mean:0.0235 var:0.023 print "logistic auc: ", sklearn.metrics.roc_auc_score(test_y,pred_logistic) This is the code of R: glm_fit <- glm(label ~ watch_cnt_7 + bid_cnt

R - using glm inside a data.table

ぃ、小莉子 提交于 2019-12-22 05:52:27
问题 I'm trying to do some glm's inside a data.table to produce modelled results split by key factors. I've been doing this sucessfully for: High level glm glm(modellingDF,formula=Outcome~IntCol + DecCol,family=binomial(link=logit)) Scoped glm with single columns modellingDF[,list(Outcome, fitted=glm(x,formula=Outcome~IntCol ,family=binomial(link=logit))$fitted ), by=variable] Scoped glm with two integer columns modellingDF[,list(Outcome, fitted=glm(x,formula=Outcome~IntCol + IntCol2 ,family

Logistic regression returns error but runs okay on reduced dataset

浪尽此生 提交于 2019-12-21 20:17:12
问题 I would appreciate your input on this a lot! I am working on a logistic regression, but it is not working for some reason: mod1<-glm(survive~reLDM2+yr+yr2+reLDM2:yr +reLDM2:yr2+NestAge0, family=binomial(link=logexp(NSSH1$exposure)), data=NSSH1, control = list(maxit = 50)) When I run the same model with less data it works! But with the complete dataset I get an error and warning messages: Error: inner loop 1; cannot correct step size In addition: Warning messages: 1: step size truncated due to

Difference in GLM results between iPython and R

∥☆過路亽.° 提交于 2019-12-21 18:08:10
问题 I'm trying to get to grips with performing regression analyses in R. Below is some random dummy data that I have generated in R, run a logistic glm in R. I have saved the data into a test file, read that into python with ipython (ipython notebook is awesome btw, only just started using it!), and then tried to run the same analyis with python. The results are very similar but they are different. I kind of would have expected them to be the same. Have I done something wrong, is there a

Difference in GLM results between iPython and R

强颜欢笑 提交于 2019-12-21 18:07:46
问题 I'm trying to get to grips with performing regression analyses in R. Below is some random dummy data that I have generated in R, run a logistic glm in R. I have saved the data into a test file, read that into python with ipython (ipython notebook is awesome btw, only just started using it!), and then tried to run the same analyis with python. The results are very similar but they are different. I kind of would have expected them to be the same. Have I done something wrong, is there a

ggplot GLM fitted curve without interaction

落花浮王杯 提交于 2019-12-21 17:34:56
问题 I want to add the fitted function from GLM on a ggplot . By default, it automatically create the plot with interaction. I am wondering, if I can plot the fitted function from the model without interaction. For example, dta <- read.csv("http://www.ats.ucla.edu/stat/data/poisson_sim.csv") dta <- within(dta, { prog <- factor(prog, levels=1:3, labels=c("General", "Academic", "Vocational")) id <- factor(id) }) plt <- ggplot(dta, aes(math, num_awards, col = prog)) + geom_point(size = 2) + geom

glmer - predict with binomial data (cbind count data)

南笙酒味 提交于 2019-12-21 04:38:11
问题 I am trying to predict values over time (Days in x axis) for a glmer model that was run on my binomial data. Total Alive and Total Dead are count data. This is my model, and the corresponding steps below. full.model.dredge<-glmer(cbind(Total.Alive,Total.Dead)~(CO2.Treatment+Lime.Treatment+Day)^3+(Day|Container)+(1|index), data=Survival.data,family="binomial") We have accounted for overdispersion as you can see in the code (1:index). We then use the dredge command to determine the best fitted