logo
down
shadow

How to fix intercept value of glm


How to fix intercept value of glm

By : Max sheh
Date : November 14 2020, 04:48 PM
help you fix your problem You are using glm(...) incorrectly, which IMO is a much bigger problem than offsets.
The main underlying assumption in least squares regression is that the error in the response is normally distributed with constant variance. If the error in Y is normally distributed, then log(Y) most certainly is not. So, while you can "run the numbers" on a fit of log(Y)~X, the results will not be meaningful. The theory of generalized linear modelling was developed to deal with this problem. So using glm, rather than fit log(Y) ~X you should fit Y~X with family=poisson. The former fits
code :
fit.incorrect <- glm(log(Y)~X,data=data2)
fit.correct   <- glm(Y~X,data=data2,family=poisson)
coef(summary(fit.incorrect))
#               Estimate Std. Error  t value     Pr(>|t|)
# (Intercept)  6.0968294 0.44450740 13.71592 0.0001636875
# X           -0.2984013 0.07340798 -4.06497 0.0152860490
coef(summary(fit.correct))
#               Estimate Std. Error   z value     Pr(>|z|)
# (Intercept)  5.8170223 0.04577816 127.06982 0.000000e+00
# X           -0.2063744 0.01122240 -18.38951 1.594013e-75
plot(Y~X,data2)
curve(exp(coef(fit.incorrect)[1]+x*coef(fit.incorrect)[2]),
      add=T,col="red")
curve(predict(fit.correct,  type="response",newdata=data.frame(X=x)),
      add=T,col="blue")
data2$b0 <- log(300)   # add the offset as a separate column
# b0 not fixed
fit <- glm(Y~X,data2,family=poisson)
plot(Y~X,data2)
curve(predict(fit,type="response",newdata=data.frame(X=x)),
      add=TRUE,col="blue")
# b0 fixed so that Y0 = 300
fit.fixed <-glm(Y~X-1+offset(b0), data2,family=poisson)
curve(predict(fit.fixed,type="response",newdata=data.frame(X=x,b0=log(300))),
      add=TRUE,col="green")


Share : facebook icon twitter icon
Find x-intercept and y-intercept of a loop in R

Find x-intercept and y-intercept of a loop in R


By : edpd1b
Date : March 29 2020, 07:55 AM
To fix this issue How can I find x-intercept and the y-intercept (all four) of a loop with R? , Here is an approxfun solution:
code :
intercepts <- function(x,y) {
  x.s <- approxfun(x[y<=0], y[y<=0])(0)
  x.n <- approxfun(x[y>=0], y[y>=0])(0)
  y.w <- approxfun(y[x<=0], x[x<=0])(0)
  y.e <- approxfun(y[x>=0], x[x>=0])(0)

  list(x.s, x.n, y.w, y.e)
}
Getting the y-axis intercept and slope from a linear regression of multiple data and passing the intercept and slope val

Getting the y-axis intercept and slope from a linear regression of multiple data and passing the intercept and slope val


By : Paul Valdivia
Date : March 29 2020, 07:55 AM
wish of those help I suppose you're looking for geom_smooth. If you call this function with the argument method = "lm", it will calculate a linear fit for all groups:
code :
ggplot(xm, aes(x = x, y = value, color = cols)) +
  geom_point(size = 3) +
  labs(x = "x", y = "y") + 
  geom_smooth(method = "lm", se = FALSE)
ggplot(xm, aes(x = x, y = value, color=cols)) +
  geom_point(size = 3) +
  labs(x = "x", y = "y") + 
  geom_smooth(method = "lm", se = FALSE, formula = y ~ poly(x, 2))
# create a list of coefficients
fits <- by(xm[-2], xm$cols, function(i) coef(lm(value ~ x, i)))

# create a data frame
data.frame(cols = names(fits), do.call(rbind, fits))

#   cols X.Intercept.         x
# y    y   -277.20000 105.40000
# s    s    -99.06667  35.13333
# t    t   -594.40000 210.80000
How to intercept Cmd+Q

How to intercept Cmd+Q


By : William Febus
Date : March 29 2020, 07:55 AM
This might help you Implement the delegate method applicationShouldTerminate of NSApplication and show a custom modal alert. Depending on the answer return NSTerminateNow, NSTerminateCancel or NSTerminateLater.
In case of NSTerminateLater you can later call [NSApp replyToApplicationShouldTerminate:YES]; to finally quit the app.
Glmnet is different with intercept=TRUE compared to intercept=FALSE and with penalty.factor=0 for an intercept in x

Glmnet is different with intercept=TRUE compared to intercept=FALSE and with penalty.factor=0 for an intercept in x


By : Bilal Eren
Date : March 29 2020, 07:55 AM
like below fixes the issue I contacted the author, who confirmed that this is a bug and added that it is on his list of bug fixes. In the meantime, a workaround is to center the regressors, e.g. with
code :
fit_centered <- glmnet(y = Y,
                       x = scale(X1, T, F),
                       intercept = F,
                       lambda = 0)
library("glmnet")

set.seed(7)

# Simulate data with 2 features
num_regressors <- 2
num_observations <- 100
X <- matrix(rnorm(num_regressors * num_observations),
            ncol = num_regressors,
            nrow = num_observations)

# Add an intercept in the right-hand side matrix: X1 = (intercept + X)
X1 <- cbind(matrix(1, ncol = 1, nrow = num_observations), X)

# Set random parameters for the features
beta <- runif(1 + num_regressors)

# Generate observations for the left-hand side
Y <- X1 %*% beta + rnorm(num_observations) / 10

# run OLS
ols <- lm(Y ~ X)
coef_ols <- coef(ols)

# Run glmnet with an intercept in the command, not in the matrix
fit <- glmnet(y = Y,
              x = X,
              intercept = T,
              penalty.factor = rep(1, num_regressors),
              lambda = 0)
coef_intercept <- coef(fit)

# run glmnet with an intercept in the matrix with a penalty
# factor of 0 for the intercept and 1 for the rest
fit_no_intercept <- glmnet(y = Y,
              x = X1,
              intercept = F,
              lambda = 0)
coef_no_intercept <- coef(fit_no_intercept)

# run glmnet with an intercept in the matrix with a penalty
# factor of 0 for the intercept and 1 for the rest
# If x is centered, it works (even though y is not centered). Center it with:
#   X1 - matrix(colMeans(X1), nrow = num_observations, ncol = 1 + num_regressors, byrow = T)
# or with
# X1_centered = scale(X1, T, F)

fit_centered <- glmnet(y = Y,
                      x = scale(X1, T, F),
                      intercept = F,
                      lambda = 0)
coef_centered <- coef(fit_centered)

# Compare all three methods in a data frame
# For lasso_intercept and the others, the index starts at 2 because
# position 1 is reserved for intercepts, which is missing in this case
comparison <- data.frame(ols = coef_ols,
                         lasso_intercept = coef_intercept[1:length(coef_intercept)],
                         lasso_no_intercept = coef_no_intercept[2:length(coef_no_intercept)],
                         lasso_centered = coef_centered[2:length(coef_centered)]
)


comparison$diff_intercept <- comparison$lasso_intercept - comparison$lasso_no_intercept
comparison$diff_centered <- comparison$lasso_centered - comparison$lasso_intercept
comparison
                  ols lasso_intercept lasso_no_intercept lasso_centered diff_intercept diff_centered
(Intercept) 0.9748302       0.9748302          0.0000000      0.0000000      0.9748302 -9.748302e-01
X1          0.6559541       0.6559541          0.7974851      0.6559541     -0.1415309  2.220446e-16
X2          0.7986957       0.7986957          0.9344306      0.7986957     -0.1357348  4.440892e-16
Intercept an instance using Unity? Globally intercept a specific type

Intercept an instance using Unity? Globally intercept a specific type


By : SoulTransfer
Date : March 29 2020, 07:55 AM
Hope that helps Ok, so here goes...
First suppose we have this domain class definition:
Related Posts Related Posts :
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • search certain number vector in R
  • R version doesn't support quartz graphic device - RStudio won't plot
  • Referencing a function parameter in R
  • How to synchronize signals using a cross-correlation and FFT in R?
  • Plotting coefficients and corresponding confidence intervals
  • passing expressions to curve() within a function
  • More effective merging of matched column with duplicates in data.table
  • Easy way to export multiple data.frame to multiple Excel worksheets
  • R Foreach Iterator - Walkforward
  • Table format and output in R
  • Restructuring data and duplicating rows in R
  • use ggplot2 to plot two lines with ribbons
  • how to plot a graph on lattice with two different colors
  • How can I keep a date formatted in R using sqldf?
  • Generating simulation data based on a specified distribution
  • Joining list of data frames in R
  • Subset data in R
  • R: How to avoid 2 'for' loops in R in this function
  • + signs appearing in console in R
  • how to create a dataframe form a lists within a list in R
  • Best way to combine and keep columns
  • Using identify and attach in a function
  • Apply function to each submatrix
  • How to assign regular strings for quarterly and monthly observation labels to the row names of a data frame?
  • Adjust hexbin legend breaks
  • Different lowess curves in plot and qplot in R
  • Extract words only with R
  • switch case: several equivalent cases expressions in r
  • R data.table to calculate a formula using a column as a variable across levels of a factor
  • how to create a line plot frame in ggplot2
  • Subset by row number within magrittr chain
  • GGPLOT - two curves in one plot in B_W mode
  • How can i build a for function for matrix?
  • How to Word-like-merge columns or rows of a data frame for displaying purposes in R?
  • How to keep all rows of a table on the same page in RMarkdown when rendering a PDF file?
  • Add transparency to GoogleMap plot (loa package)
  • replace a column in a dataframe given a corresponding vector in r
  • subset data and plot this subsetted data with Shiny
  • How can i count the numbers in every subset?
  • Request URL failed/timeout in R
  • IF then do end equivalent in r... EDIT: in dplyr
  • how to check if each cells (list) of a column of a dataframe are unique in R?
  • Column widths not aligned with table data in pander tables sent from R with sendmailr
  • Getting the value of a Variable which has its name based upon another variable (in R)
  • Web Page Click Through Heat Map using R
  • Add a label to map at each leg start
  • R Caret Random Forest view miss-classified
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org