All Questions
586
questions
89
votes
2
answers
65k
views
Reusing a Model Built in R
When building a model in R, how do you save the model specifications such that you can reuse it on new data? Let's say I build a logistic regression on historical data but won't have new observations ...
76
votes
11
answers
63k
views
All Levels of a Factor in a Model Matrix in R
I have a data.frame consisting of numeric and factor variables as seen below.
testFrame <- data.frame(First=sample(1:10, 20, replace=T),
Second=sample(1:20, 20, replace=T), Third=sample(...
37
votes
1
answer
16k
views
model.matrix generates fewer rows than original data.frame
Why doesn't a model matrix necessarily have the same number of rows as the data frame?
mergem = model.matrix(as.formula(paste(response, '~ .')), data=mergef)
dim(mergef)
# [1] 115562 71
dim(...
18
votes
3
answers
16k
views
How to fit a random effects model with Subject as random in R?
Given data of the following form
myDat = structure(list(Score = c(1.84, 2.24, 3.8, 2.3, 3.8, 4.55, 1.13,
2.49, 3.74, 2.84, 3.3, 4.82, 1.74, 2.89, 3.39, 2.08, 3.99, 4.07,
1.93, 2.39, 3.63, 2.55, 3....
12
votes
5
answers
15k
views
Hyper-parameter tuning using pure ranger package in R
Love the speed of the ranger package for random forest model creation, but can't see how to tune mtry or number of trees. I realize I can do this via caret's train() syntax, but I prefer the speed ...
9
votes
3
answers
3k
views
Format model display in texreg or stargazer R as scientific
I just ran a statisitical model and i want it to display the results of the model as a table using stargazer. However, the large numbers are displayed in full.
fit2<-lm(A~B,data=C)
stargazer(fit2,...
8
votes
1
answer
4k
views
Manually build logistic regression model for prediction in R
I'm attempting to test a logistic regression model (e.g. 3 coefficients for 3 predictor variables, X1,X2,X3), on a dataset. I'm aware of how to test a model after i created the model object using, ...
7
votes
3
answers
11k
views
GLM with autoregressive term to correct for serial correlation
I have a stationary time series to which I want to fit a linear model with an autoregressive term to correct for serial correlation, i.e. using the formula At = c1*Bt + c2*Ct + ut, where ut = r*ut-1 + ...
7
votes
2
answers
20k
views
How to extract a p-value when performing anova() between two glm models in R
So, I'm trying to compare two models, fit1 and fit2.
Initially, I was just doing anova(fit1,fit2), and this yielded output that I understood (including a p-value).
However, when I switched my models ...
6
votes
1
answer
9k
views
Autocorrelation in Generalized Additive Models (GAM)
I have a time series dataset for water temperature, air temperature, and flow rate in a river. I have created a GAM model to predict water temperature based on air temp and flow. However, I have not ...
6
votes
1
answer
3k
views
R Stepwise alternative for automatic model selection for panel regression models (plm)
Im running a panel regression (randon effects) and i have several regressor candidates.
X.panel3.form = as.formula(c("value ~ ",paste(X.panel3.cols,collapse="+")))
> X.panel3.form
value ~ SMB + ...
6
votes
0
answers
6k
views
Covariance models with lme and gls [closed]
I'm trying to fit several covariance models using gls and lme. My aim is to identify which covariance model fits my data better. I'm afraid, however, that I'm not specifying the code properly. Could ...
5
votes
5
answers
22k
views
Command for finding the best linear model in R
Is there a way to get R to run all possible models (with all combinations of variables in a dataset) to produce the best/most accurate linear model and then output that model?
I feel like there is a ...
5
votes
1
answer
2k
views
R: using factor variables in nlme function
library(nlme)
model <- nlme(height ~ (R0) + 1,
data = Loblolly,
fixed = list(R0 ~ 1),
random = list(Seed = pdDiag(list(R0 ~ 1))),
start = ...
4
votes
1
answer
19k
views
R - Model with a lot of dummy variables
If I have a column in a data set that has multiple variables how would I go about creating these dummy variables.
Example:
Lets say that I have a column named color it has: Red, Green, Yellow, Blue, ...
4
votes
1
answer
3k
views
Does the varIdent function, used in LME work fine?
I would be glad if somebody could help me to solve this problem. I have data with repeated measurements design, where we tested a reaction of birds (time.dep) before and after the infection (exper). ...
4
votes
1
answer
261
views
Any difference between modelobject$residuals and residuals(modelobject) in R package pscl?
I'm trying to build some models using Zero-Inflated Poisson regression using pscl package and after having manipulated the output object which turns to be zeroinfl, I find that doing residuals(fm_zip) ...
4
votes
1
answer
1k
views
Survey Weighted Random Effects Logit Model in R
I am trying to predict a binary outcome with a model that includes a random effect using survey data. I've included a description of the sampling design below, so feel free to comment on my survey ...
4
votes
1
answer
229
views
Unexpected error when looping the glmer function with the effects package
I am trying to pass the effect function from the effects package together with a (gl)merMod object from the lme4 package through a lapply loop and encounter an error I do not expect. It seems that the ...
4
votes
1
answer
2k
views
Extract data from Partial least square regression on R
I want to use the partial least squares regression to find the most representative variables to predict my data.
Here is my code:
library(pls)
potion<-read.table("potion-insomnie.txt",header=T)
...
4
votes
2
answers
519
views
igraph'ing nested models
Running e.g. cv.glmnet on a dataset gives me (by default) 100 different models. Now, if my dataset had missing data, I could do multiple imputation (say 10 imputations) and run cv.glmnet on each of ...
3
votes
1
answer
18k
views
Missing values in object in R lme
I am working on a simple model in R:
fit<-lme(x~y, data, random=~1|subject)
But I keep getting this error:
Error in na.fail.default(list(h1 = c(103L, 20L, 34L, 85L, 47L, 136L,
76L, : ...
3
votes
2
answers
807
views
Creating multiple Bias-Variance Tradeoff plots in R
I am relatively new to R. I would like to know how to create the following graphic. I have been stuck for over two hours.
Suppose the red line - the true relationship - is y = x^2.
Suppose I want ...
3
votes
2
answers
4k
views
Plot the F-distribution from an lm object in R
Suppose we have two variables that we wish to build a model from:
set.seed(10239)
x <- rnorm(seq(1,100,1))
y <- rnorm(seq(1,100,1))
model <- lm(x~y)
class(model)
# [1] "lm"
summary(model)
#...
3
votes
1
answer
6k
views
f-test for two models in R
I would like to compare two models using f-test fitting my data. For each model I performed Monte-Carlo simulation that provided statistical estimation for each model parameter and rms fit error. I ...
3
votes
2
answers
162
views
What does "~." mean in R?
it's the first time I've used the model.matrix command on R. I'm posting the code from an example found in a book:
model.matrix(~., data)
I don't understand what ~. is.
Looking on the internet I saw ...
3
votes
8
answers
3k
views
cbind error while building Pareto/NBD Model in R (BTYD Package)
I am building a CLV model using the BTYD package and I have hit a roadblock that I just can't seem to get around.
I have been carefully following the directions from Section 2.1 in this tutorial. ...
3
votes
2
answers
2k
views
plot line over last part of barplot
I have a barplot for which the second half should fit this formula:
y~axexp(-b*x^2). Now I want to plot the entire barplot and display the fitted model over the last part of the barplot as it only ...
3
votes
1
answer
106
views
How to extract Lambda and Nu parameters from glmmTMB Conway-Maxwell Poisson regression model
I'm trying to build Conway Maxwell Poisson model with glmmTMB package with model's family = "compois". The model works okay but I cannot find any output from the model that expresses the ...
3
votes
1
answer
3k
views
Predict using a linear model in R, given data points [closed]
I have set up the model:
selected.model<-(ES~Area+Elevation+DistSC+I(Elevation^2)+
(Elevation*DistSC)+(Area*Elevation))
I want to predict ES given the data points:
Area=0.02, ...
3
votes
1
answer
35
views
Enhanced likelihood ratio for comparison of models obtained with the lm() function
I am looking for a function in R to compare models created with the lm() function, with the following statistics:
Models df AIC BIC logLik Test L.ratio p.value
1 model1 6 161.65 170.44 -74....
3
votes
1
answer
658
views
How to add covariates in loess and spline regression and then plot it in r with ggplot2
I know how to plot loess and spline regression with just one independent variable.
library(tidyverse)
# loess
ggplot(mtcars, aes(x = mpg, y = drat)) +
geom_point() +
geom_smooth(method = '...
3
votes
1
answer
892
views
R: predict() for zero-inflated model does not return se.fit
I am trying to compute Confidence Intervals for zero-inflated models which have been set up using the function zeroinfl()
If I compute them from a linear model or GLM using the function
predict(glm,...
3
votes
0
answers
251
views
Draw residuals for linear, major axis and standardized major axis regressions
I have been going in circles trying to replicate a figure from Warton et al (2006) in R.
I want to highlight how the direction of the residuals are drawn for a presentation and have a reproducible ...
3
votes
0
answers
409
views
How to weight a kernel function in ksvm of kernlab package in R?
I am rewriting a RBF kernel function:
install.packages("kernlab")
library(kernlab)
rbf <- function(x, y) {
gamma<-0.5
exp(-0.5*norm((as.matrix(x)-as.matrix(y)),"f"))
}
class(rbf) &...
3
votes
1
answer
1k
views
Linear model/lmList with nested/multiple group categories?
I am trying to build a model for monthly energy consumption based on weather, grouped by location (there are ~1100) AND year (I would like to do it from 2011-2014). The data is called factin and looks ...
3
votes
1
answer
4k
views
How to Separate a Dataset in R
I read a csv file into R.
There are options like quantity, price, size, and some others.
The same product has different size and therefore different prices.
How can I separate the dataset based on ...
2
votes
2
answers
2k
views
How to use the pdp in R to compute 3d partial dependence plots?
I have a Random forest model in R similar to this:
library("randomForest")
library("caret")
library("pdp")
data("cars")
my_data<-cars[1:5]
my_rf <- ...
2
votes
3
answers
405
views
How to fit multiple interaction models in a loop?
Lets say i have 3 response variables A,C and M and i want to fit a model for all possible models ie fit Y ~ A, Y ~ C, Y ~ M, Y ~ A * C, Y ~ A * M, Y ~ C * M, etc. Is there a quick way to do this ...
2
votes
3
answers
143
views
Improve efficiency of fitting many models
Thanks to those who helped me with previous questions about coefficients and p values from many models. Now, I can get all coefficients, p values, and AIC values from many models.
md <- "mpg ~ ...
2
votes
2
answers
12k
views
Using the ns() function from the splines package inside glm()
I'm trying to use the ns() function from the splines package with a poisson GLM I am using to test for significance of particulate matter concentration (pm.lag0) on health outcomes (Freq):
> ...
2
votes
1
answer
86
views
Extracting R2 from models and storing them into a vector in R?
I am building models in R, and would like to extract the R2 values of those models and store them into a vector.
Here is a simplified dataframe showing the speed of 6 bears in relation to their sex, ...
2
votes
3
answers
2k
views
How do I extract parameters from listed models in R?
I am trying to extract the parameters of a model I have designed for my work, but am having trouble doing so. I will use the iris data provided with R as an example data set.
I pull in the data:
...
2
votes
2
answers
176
views
Get p values for a specific variable in many models with all possible combinations of other independent variables
I am trying to run many regression models with all possible combinations of a set of independent variables.
In this example, I am interested in the coefficients of cyl with all possible ...
2
votes
1
answer
18k
views
How to solve "rank-deficient fit may be misleading error" on my linear model?
I have a problem when I use my model to do some prediction, R shows this message Warning message prediction from a rank-deficient fit may be misleading, how can I solve it? I think my model is correct ...
2
votes
2
answers
3k
views
How to draw the Gaussian graphical model in R
I have got the Correlation coefficient matrix R, and the Partial correlation coefficient matrix Rp, then how could I draw the Gaussian graphical model in R?
It would be better if recommending some ...
2
votes
3
answers
2k
views
How to replace vector tidier
I am looking to find another function that can replace the broom::tidy() function after it get removed. Here is what the broom package warning says:
Tidy Atomic Vectors
Vector tidiers are deprecated ...
2
votes
1
answer
667
views
Export glm train fit summary from caret with stargazer?
I want to use the package "stargazer" in order to export the fit summary of a train with caret.
e.g.
LogReg <- train(
Target ~ .,
data = DecileFramesTrain[[i]], method="glm", family="binomial",
...
2
votes
1
answer
4k
views
Fully inspect an object in R
Question upfront: If use summary, I get a much more detailed view on my model than just print produces. In R, how can I know what extra information my object holds, such as revealed by summary, which ...
2
votes
1
answer
3k
views
How to implement R model in C++ code
I have a model in R:
> s1 <- toys[1:10000,]
> model <- glm(V11~V2+V3+V5+V7+V8+V9+V10,gaussian,s1)
> model
Call: glm(formula = V11 ~ V2 + V3 + V5 + V7 + V8 + V9 + V10, family = ...