lm

Get coefficients estimated by maximum likelihood into a stargazer table

冷暖自知 提交于 2019-12-03 06:26:11
问题 Stargazer produces very nice latex tables for lm (and other) objects. Suppose I've fit a model by maximum likelihood. I'd like stargazer to produce a lm-like table for my estimates. How can I do this? Although it's a bit hacky, one way might be to create a "fake" lm object containing my estimates -- I think this would work as long as summary(my.fake.lm.object) works. Is that easily doable? An example: library(stargazer) N <- 200 df <- data.frame(x=runif(N, 0, 50)) df$y <- 10 + 2 * df$x + 4 *

How to predict x values from a linear model (lm)

人盡茶涼 提交于 2019-12-03 06:21:31
I have this data set: x <- c(0, 40, 80, 120, 160, 200) y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60) I calculated a linear model using lm() : model <- lm(y ~ x) I want know the predicted values of x if I have new y values, e.g. ynew <- c(5.5, 4.5, 3.5) , but if I use the predict() function, it calculates only new y values. How can I predict new x values if I have new y values? Since this is a typical problem in chemistry (predict values from a calibration), package chemCal provides inverse.predict . However, this function is limited to "univariate model object[s] of class lm or rlm with model

Plot fitted line within certain range R

雨燕双飞 提交于 2019-12-03 05:40:44
Using R, I would like to plot a linear relationship between two variables, but I would like the fitted line to be present only within the range of the data. For example, if I have the following code, I would like the line to exist only from x and y values of 1:10 (with default parameters this line extends beyond the range of data points). x <- 1:10 y <- 1:10 plot(x,y) abline(lm(y~x)) Instead of using abline() , (a) save the fitted model, (b) use predict.lm() to find the fitted y-values corresponding to x=1 and x=10, and then (c) use lines() to add a line between the two points: f <- lm(y~x) X

regressions with xts in R

人盡茶涼 提交于 2019-12-03 05:01:46
Is there a utility to run regressions using xts objects of the following type: lm(y ~ lab(x, 1) + lag(x, 2) + lag(x,3), data=as.data.frame(coredata(my_xts))) where my_xts is an xts object that contains an x and a y . The point of the question is is there a way to avoid doing a bunch of lags and merges to have a data.frame with all the lags? I think that the package dyn works for zoo objects so i would expect it to work the same way with xts but though there might be something updated. G. Grothendieck The dyn and dynlm packages can do that with zoo objects. In the case of dyn just write dyn$lm

Performing lm() and segmented() on multiple columns in R

亡梦爱人 提交于 2019-12-03 04:05:42
I am trying to perform lm() and segmented() in R using the same independent variable (x) and multiple dependent response variables (Curve1, Curve2, etc.) one by one. I wish to extract the estimated break point and model coefficients for each response variable. I include an example of my data below. x Curve1 Curve2 Curve3 1 -0.236422 98.8169 95.6828 101.7910 2 -0.198083 98.3260 95.4185 101.5170 3 -0.121406 97.3442 94.8899 100.9690 4 0.875399 84.5815 88.0176 93.8424 5 0.913738 84.1139 87.7533 93.5683 6 1.795530 73.3582 78.1278 82.9956 7 1.833870 72.8905 77.7093 82.7039 8 1.872200 72.4229 77.3505

How to obtain RMSE out of lm result?

别说谁变了你拦得住时间么 提交于 2019-12-02 21:06:17
I know there is a small difference between $sigma and the concept of root mean squared error . So, i am wondering what is the easiest way to obtain RMSE out of lm function in R ? res<-lm(randomData$price ~randomData$carat+ randomData$cut+randomData$color+ randomData$clarity+randomData$depth+ randomData$table+randomData$x+ randomData$y+randomData$z) length(coefficients(res)) contains 24 coefficient, and I cannot make my model manually anymore. So, how can I evaluate the RMSE based on coefficients derived from lm ? Residual sum of squares: RSS <- c(crossprod(res$residuals)) Mean squared error:

R - apply lm on each data frame row

对着背影说爱祢 提交于 2019-12-02 20:15:52
问题 I am trying to apply a simple linear regression between two columns of a data frame, for every row. After some research I feel like I am almost there, but my function still doesn't work. Please take a look: set.seed(1) DF <- data.frame(A=rnorm(50, 100, 3), B=rnorm(50, 100, 3)) resultlist <- apply(DF, 1, function(y) lm(y ~ x)) resultcoeffs <- apply(DF, 1, function(y) lm(y ~ x)$coefficients) Any tip on how to achieve that? Thanks in advance. 回答1: It is just one observation per row. Note that

Get coefficients estimated by maximum likelihood into a stargazer table

随声附和 提交于 2019-12-02 19:53:05
Stargazer produces very nice latex tables for lm (and other) objects. Suppose I've fit a model by maximum likelihood. I'd like stargazer to produce a lm-like table for my estimates. How can I do this? Although it's a bit hacky, one way might be to create a "fake" lm object containing my estimates -- I think this would work as long as summary(my.fake.lm.object) works. Is that easily doable? An example: library(stargazer) N <- 200 df <- data.frame(x=runif(N, 0, 50)) df$y <- 10 + 2 * df$x + 4 * rt(N, 4) # True params plot(df$x, df$y) model1 <- lm(y ~ x, data=df) stargazer(model1, title="A Model")

predict.lm() in a loop. warning: prediction from a rank-deficient fit may be misleading

偶尔善良 提交于 2019-12-02 17:29:57
This R code throws a warning # Fit regression model to each cluster y <- list() length(y) <- k vars <- list() length(vars) <- k f <- list() length(f) <- k for (i in 1:k) { vars[[i]] <- names(corc[[i]][corc[[i]]!= "1"]) f[[i]] <- as.formula(paste("Death ~", paste(vars[[i]], collapse= "+"))) y[[i]] <- lm(f[[i]], data=C1[[i]]) #training set C1[[i]] <- cbind(C1[[i]], fitted(y[[i]])) C2[[i]] <- cbind(C2[[i]], predict(y[[i]], C2[[i]])) #test set } I have a training data set (C1) and a test data set (C2). Each one has 129 variables. I did k means cluster analysis on the C1 and then split my data set

predict lm function in R (multiple linear regression)

只谈情不闲聊 提交于 2019-12-02 16:18:21
问题 I did a multiple linear regression in R using the function lm and I want to use it to predict several values. So I'm trying to use the function predict() . Here is my code: new=data.frame(t=c(10, 20, 30)) v=1/t LinReg<-lm(p ~ log(t) + v) Pred=predict(LinReg, new, interval="confidence") So I would like to predict the values of p when t=c(10,20,30...) . However, this is not working and I don't see why. The error message I get is: "Error in model.frame.default(Terms, newdata, na.action = na