lm

How can I calculate the slope of multiple subsets of a data frame more efficiently? [duplicate]

纵然是瞬间 提交于 2021-02-07 20:56:21
问题 This question already has answers here : Linear Regression and group by in R (10 answers) Closed 4 years ago . I have a data set that contains the optical absorption ( a ) across a range of wavelengths ( wl ) for 16 different samples ( bod ) on 5 different days ( day ). The dput output for samples ( bod ) 1 - 3 is below. I need the slope of the natural log of the absorption by wavelength for each sample on each day. My current approach has been to do this manually: # calculate and extract the

How to get regression coefficients and model fits using correlation or covariance matrix instead of data frame using R?

你离开我真会死。 提交于 2021-02-07 18:18:25
问题 I want to be able to regression coefficients from multiple linear regression by supplying a correlation or covariance matrix instead of a data.frame. I realise you lose some information relevant to determining the intercept and so on, but it should even the correlation matrix should be sufficient for getting standardised coefficients and estimates of variance explained. So for example, if you had the following data # get some data library(MASS) data("Cars93") x <- Cars93[,c("EngineSize",

r for loop for regression lm(y~x)

情到浓时终转凉″ 提交于 2021-02-07 08:47:48
问题 Example: df <- data.frame(A=1:5, B=2:6, C=3:7,D=4:8,E=5:9,F=6:10) I want make a regression loop lm(y,x) using like y the col 1 and 2 and like x the rest of the cols. my idea: lmf <- function (y,x) { f <- lm(y ~ x, data=df) cbind(summary(f)$r.squared,summary(f)$coefficients) } for(y in 1:3) { R<- apply(df[,3:6], 2, lmf(y,x)); R } error: Error in model.frame.default(formula = y ~ x, data = df, drop.unused.levels = TRUE) : variable lengths differ (found for 'x') I give this example very small

Replace lm coefficients in [r]

泄露秘密 提交于 2021-02-07 04:26:01
问题 Is it possible to replace coefficients in lm object? I thought the following would work # sample data set.seed(2157010) x1 <- 1998:2011 x2 <- x1 + rnorm(length(x1)) y <- 3*x1 + rnorm(length(x1)) fit <- lm( y ~ x1 + x2) # view origional coefficeints coef(fit) # replace coefficent with new values fit$coef(fit$coef[2:3]) <- c(5, 1) # view new coefficents coef(fit) Any assistance would be greatly appreciated 回答1: Your code is not reproducible, as there's few errors in your code. Here's corrected

Replace lm coefficients in [r]

六眼飞鱼酱① 提交于 2021-02-07 04:24:15
问题 Is it possible to replace coefficients in lm object? I thought the following would work # sample data set.seed(2157010) x1 <- 1998:2011 x2 <- x1 + rnorm(length(x1)) y <- 3*x1 + rnorm(length(x1)) fit <- lm( y ~ x1 + x2) # view origional coefficeints coef(fit) # replace coefficent with new values fit$coef(fit$coef[2:3]) <- c(5, 1) # view new coefficents coef(fit) Any assistance would be greatly appreciated 回答1: Your code is not reproducible, as there's few errors in your code. Here's corrected

Linear model (lm) when dependent variable is a factor/categorical variable?

我与影子孤独终老i 提交于 2021-02-04 17:15:11
问题 I want to do linear regression with the lm function. My dependent variable is a factor called AccountStatus : 1:0 days in arrears, 2:30-60 days in arrears, 3:60-90 days in arrears and 4:90+ days in arrears. (4) As independent variable I have several numeric variables: Loan to value , debt to income and interest rate . Is it possible to do a linear regression with these variables? I looked on the internet and found something about dummy's, but those were all for the independent variable. This

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases Calls: lm -> lm.fit

十年热恋 提交于 2021-01-29 06:13:05
问题 I am trying to run an R application, but I receive the following first error : Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Calls: lm -> lm.fit The code which generates the error is : pppb = lm(Exchange.rate.change ~ Inflation.difference) I am new to R and is really hard for me to find the mistake, so any help it is really appreciated. This is a minimal data set: Country Inflation.difference Exchange.rate.change Developed Australia -1.235100000e

Fail to add linear regression line in barplot

雨燕双飞 提交于 2021-01-28 18:30:38
问题 I have some data about the percentages of temperature for different time periods and I want to create a barplot showing those percentages and then add a linear regression line showing the trend. Although i manage to get the first graph, I fail to add a straight linear regression line Basically I try to make a barplot with these tx_1 data tx_1<-c(0.055,0.051,0.057,0.049,0.061,0.045) mypath<-file.path("C:\\tx5\\1.jpeg") jpeg(file = mypath,width = 1200, height = 600) plot.dim<-barplot(get(name),

Passing strings into 'contrasts' argument of lme/lmer

时光怂恿深爱的人放手 提交于 2021-01-28 11:47:09
问题 I am writing a sub-routine to return output of longitudinal mixed-effects models. I want to be able to pass elements from lists of variables into lme/lmer as the outcome and predictor variables. I would also like to be able to specify contrasts within these mixed-effects models, however I am having trouble with getting the contrasts() argument to recognise the strings as the variable names referred to in the model specification within the same lme/lme4 call. Here's some toy data, set.seed(345

Fit multiple Gompertz curves and skip errors in R (nlsList and SSgompertz)

China☆狼群 提交于 2021-01-28 05:11:04
问题 I'm trying to fit hundreds of gompertz-shaped curves using SSgompertz. The dataset has three columns with "x" and "y" values and a coded column to separate the data into different samples: "GROUPING". Later, the parameters will be used to determine x from a fixed point on the y-axis for all samples (fit point methods) using predict(). I managed to fit multiple polynomials to the data before feeding the parameters into predict() using this code: Parameters<-lmList(x~poly(y,3,raw=TRUE)|GROUPING