r lm parameter estimates
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
error variable length differs
I am confused with this error and I don't know what to do.
n1<-20
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
e<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
modelfit1<-lm(y~ b0 + b1*x + e)
Error in model.frame.default(formula = y ~ b0 + b1 * x + e:
variable lengths differ (found for 'b0')
edited:
I am working on such case where n=20, the parameters b0=0, and b=1 are true and the independent and the error are normally distributed with mean=0 and sd=1.
Is this possible?
Thanks a lot!
r parameters lm
add a comment |
error variable length differs
I am confused with this error and I don't know what to do.
n1<-20
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
e<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
modelfit1<-lm(y~ b0 + b1*x + e)
Error in model.frame.default(formula = y ~ b0 + b1 * x + e:
variable lengths differ (found for 'b0')
edited:
I am working on such case where n=20, the parameters b0=0, and b=1 are true and the independent and the error are normally distributed with mean=0 and sd=1.
Is this possible?
Thanks a lot!
r parameters lm
2
What do you want to do?
– coffeinjunky
Jan 3 at 16:00
1
Hello, welcome to StackOverflow. I think your simulated example does not really match what you want to do. Maybe this post would help? stats.stackexchange.com/questions/99924/…
– Vincent Guillemot
Jan 3 at 16:01
1
A comment on where the error comes from: R thinks that the model you want to fit contains a response variable y (so far so good), and a complicated combination of explanatory variables (one called b0, one called b1, one called x, one interaction term between the variables x and b1 and a final term that I don't even know how R will interpret it). If your intention was to generate a simple dataset to fit a simple linear model on (and I think it was your intention), then you need to fix your simulated data.
– Vincent Guillemot
Jan 3 at 16:10
@coffeinjunky hello, i want to see the estimate standard error of the parameter b0 and b1.
– bel
Jan 3 at 16:28
add a comment |
error variable length differs
I am confused with this error and I don't know what to do.
n1<-20
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
e<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
modelfit1<-lm(y~ b0 + b1*x + e)
Error in model.frame.default(formula = y ~ b0 + b1 * x + e:
variable lengths differ (found for 'b0')
edited:
I am working on such case where n=20, the parameters b0=0, and b=1 are true and the independent and the error are normally distributed with mean=0 and sd=1.
Is this possible?
Thanks a lot!
r parameters lm
error variable length differs
I am confused with this error and I don't know what to do.
n1<-20
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
e<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
modelfit1<-lm(y~ b0 + b1*x + e)
Error in model.frame.default(formula = y ~ b0 + b1 * x + e:
variable lengths differ (found for 'b0')
edited:
I am working on such case where n=20, the parameters b0=0, and b=1 are true and the independent and the error are normally distributed with mean=0 and sd=1.
Is this possible?
Thanks a lot!
r parameters lm
r parameters lm
edited Jan 3 at 16:35
bel
asked Jan 3 at 15:56
belbel
32
32
2
What do you want to do?
– coffeinjunky
Jan 3 at 16:00
1
Hello, welcome to StackOverflow. I think your simulated example does not really match what you want to do. Maybe this post would help? stats.stackexchange.com/questions/99924/…
– Vincent Guillemot
Jan 3 at 16:01
1
A comment on where the error comes from: R thinks that the model you want to fit contains a response variable y (so far so good), and a complicated combination of explanatory variables (one called b0, one called b1, one called x, one interaction term between the variables x and b1 and a final term that I don't even know how R will interpret it). If your intention was to generate a simple dataset to fit a simple linear model on (and I think it was your intention), then you need to fix your simulated data.
– Vincent Guillemot
Jan 3 at 16:10
@coffeinjunky hello, i want to see the estimate standard error of the parameter b0 and b1.
– bel
Jan 3 at 16:28
add a comment |
2
What do you want to do?
– coffeinjunky
Jan 3 at 16:00
1
Hello, welcome to StackOverflow. I think your simulated example does not really match what you want to do. Maybe this post would help? stats.stackexchange.com/questions/99924/…
– Vincent Guillemot
Jan 3 at 16:01
1
A comment on where the error comes from: R thinks that the model you want to fit contains a response variable y (so far so good), and a complicated combination of explanatory variables (one called b0, one called b1, one called x, one interaction term between the variables x and b1 and a final term that I don't even know how R will interpret it). If your intention was to generate a simple dataset to fit a simple linear model on (and I think it was your intention), then you need to fix your simulated data.
– Vincent Guillemot
Jan 3 at 16:10
@coffeinjunky hello, i want to see the estimate standard error of the parameter b0 and b1.
– bel
Jan 3 at 16:28
2
2
What do you want to do?
– coffeinjunky
Jan 3 at 16:00
What do you want to do?
– coffeinjunky
Jan 3 at 16:00
1
1
Hello, welcome to StackOverflow. I think your simulated example does not really match what you want to do. Maybe this post would help? stats.stackexchange.com/questions/99924/…
– Vincent Guillemot
Jan 3 at 16:01
Hello, welcome to StackOverflow. I think your simulated example does not really match what you want to do. Maybe this post would help? stats.stackexchange.com/questions/99924/…
– Vincent Guillemot
Jan 3 at 16:01
1
1
A comment on where the error comes from: R thinks that the model you want to fit contains a response variable y (so far so good), and a complicated combination of explanatory variables (one called b0, one called b1, one called x, one interaction term between the variables x and b1 and a final term that I don't even know how R will interpret it). If your intention was to generate a simple dataset to fit a simple linear model on (and I think it was your intention), then you need to fix your simulated data.
– Vincent Guillemot
Jan 3 at 16:10
A comment on where the error comes from: R thinks that the model you want to fit contains a response variable y (so far so good), and a complicated combination of explanatory variables (one called b0, one called b1, one called x, one interaction term between the variables x and b1 and a final term that I don't even know how R will interpret it). If your intention was to generate a simple dataset to fit a simple linear model on (and I think it was your intention), then you need to fix your simulated data.
– Vincent Guillemot
Jan 3 at 16:10
@coffeinjunky hello, i want to see the estimate standard error of the parameter b0 and b1.
– bel
Jan 3 at 16:28
@coffeinjunky hello, i want to see the estimate standard error of the parameter b0 and b1.
– bel
Jan 3 at 16:28
add a comment |
2 Answers
2
active
oldest
votes
I suggest you put everything in a data.frame
and deal with it that way:
set.seed(2)
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
d <- data.frame(y,b0,b1,x,e=rnorm(20,0,1))
head(d)
# y b0 b1 x e
# 1 -0.89691455 0 1 2.090819205 -0.3835862
# 2 0.18484918 0 1 -1.199925820 -1.9591032
# 3 1.58784533 0 1 1.589638200 -0.8417051
# 4 -1.13037567 0 1 1.954651642 1.9035475
# 5 -0.08025176 0 1 0.004937777 0.6224939
# 6 0.13242028 0 1 -2.451706388 1.9909204
Now things work nicely:
modelfit1 <- lm(y~b0+b1*x+e, data=d)
modelfit1
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Coefficients:
# (Intercept) b0 b1 x e b1:x
# 0.19331 NA NA -0.06752 0.02240 NA
summary(modelfit1)
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Residuals:
# Min 1Q Median 3Q Max
# -2.5006 -0.4786 -0.1425 0.6211 1.8488
# Coefficients: (3 not defined because of singularities)
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.19331 0.25013 0.773 0.450
# b0 NA NA NA NA
# b1 NA NA NA NA
# x -0.06752 0.21720 -0.311 0.760
# e 0.02240 0.20069 0.112 0.912
# b1:x NA NA NA NA
# Residual standard error: 1.115 on 17 degrees of freedom
# Multiple R-squared: 0.006657, Adjusted R-squared: -0.1102
# F-statistic: 0.05697 on 2 and 17 DF, p-value: 0.9448
thank you. though i am really confused how the parameters b0 and b1 have NA.
– bel
Jan 3 at 16:39
In this case, they are singular and invariant, so they can have no impact on the model. Realize that the model is already including an intercept ((Intercept)
), which your formula is trying to mimick withb0
; if you want to exclude the automatic intercept in the formula, you'll need to subtract one in the formula, as iny ~ b0 + b1*x + e - 1
, which now gives you a meaningfulb1
but stillb0
is invariant ... which doesn't help your model. If this is for a class, I suggest you go ask the professor about what's happening here.
– r2evans
Jan 3 at 16:53
BTW: addingb1
is also meaningless here, since thex
line in thesummary
output is giving you what it thinks the coefficient ofx
should be given this model. If you really want to force it, then perhaps you should not be includingx
in the model.
– r2evans
Jan 3 at 16:54
In the long run, I'd think a more meaningful formula could bey ~ x + e
.
– r2evans
Jan 3 at 16:55
2
Just a note: in addition, one could also useI(b1*x)
in the model formula as it would then not be interpreted as an interaction term, but as the actual product of these two columns. Not clear though that this is what is the OPs intent.
– coffeinjunky
Jan 3 at 16:58
|
show 1 more comment
I may be wrong, but I believe you want to simulate an outcome and then estimate it's parameters. If that is true, you would rather do the following:
n1 <- 20
m1 <- 0
sd1<- 1
b0 <- 0
b1 <- 1
x <- rnorm(n1,m1, sd1)
e <- rnorm(n1,m1, sd1)
y <- b0 + b1*x + e
summary(lm(y~x))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.66052 -0.40203 0.05659 0.44115 1.38798
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.3078 0.1951 -1.578 0.132
x 1.1774 0.2292 5.137 6.9e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.852 on 18 degrees of freedom
Multiple R-squared: 0.5945, Adjusted R-squared: 0.572
F-statistic: 26.39 on 1 and 18 DF, p-value: 6.903e-05
And in case you want to do this multiple times, consider the following:
repetitions <- 5
betas <- t(sapply(1:repetitions, function(i){
y <- b0 + b1*x + rnorm(n1,m1, sd1)
coefficients(lm(y~x))
}))
betas
(Intercept) x
[1,] 0.21989182 0.8185690
[2,] -0.12820726 0.7289041
[3,] -0.27596844 0.9794432
[4,] 0.06145306 1.0575050
[5,] -0.31429950 0.9984262
Now you can look at the mean of the estimated betas:
colMeans(betas)
(Intercept) x
-0.08742606 0.91656951
and the variance-covariance matrix:
var(betas)
(Intercept) x
(Intercept) 0.051323041 -0.007976803
x -0.007976803 0.018834711
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025722%2fr-lm-parameter-estimates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I suggest you put everything in a data.frame
and deal with it that way:
set.seed(2)
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
d <- data.frame(y,b0,b1,x,e=rnorm(20,0,1))
head(d)
# y b0 b1 x e
# 1 -0.89691455 0 1 2.090819205 -0.3835862
# 2 0.18484918 0 1 -1.199925820 -1.9591032
# 3 1.58784533 0 1 1.589638200 -0.8417051
# 4 -1.13037567 0 1 1.954651642 1.9035475
# 5 -0.08025176 0 1 0.004937777 0.6224939
# 6 0.13242028 0 1 -2.451706388 1.9909204
Now things work nicely:
modelfit1 <- lm(y~b0+b1*x+e, data=d)
modelfit1
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Coefficients:
# (Intercept) b0 b1 x e b1:x
# 0.19331 NA NA -0.06752 0.02240 NA
summary(modelfit1)
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Residuals:
# Min 1Q Median 3Q Max
# -2.5006 -0.4786 -0.1425 0.6211 1.8488
# Coefficients: (3 not defined because of singularities)
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.19331 0.25013 0.773 0.450
# b0 NA NA NA NA
# b1 NA NA NA NA
# x -0.06752 0.21720 -0.311 0.760
# e 0.02240 0.20069 0.112 0.912
# b1:x NA NA NA NA
# Residual standard error: 1.115 on 17 degrees of freedom
# Multiple R-squared: 0.006657, Adjusted R-squared: -0.1102
# F-statistic: 0.05697 on 2 and 17 DF, p-value: 0.9448
thank you. though i am really confused how the parameters b0 and b1 have NA.
– bel
Jan 3 at 16:39
In this case, they are singular and invariant, so they can have no impact on the model. Realize that the model is already including an intercept ((Intercept)
), which your formula is trying to mimick withb0
; if you want to exclude the automatic intercept in the formula, you'll need to subtract one in the formula, as iny ~ b0 + b1*x + e - 1
, which now gives you a meaningfulb1
but stillb0
is invariant ... which doesn't help your model. If this is for a class, I suggest you go ask the professor about what's happening here.
– r2evans
Jan 3 at 16:53
BTW: addingb1
is also meaningless here, since thex
line in thesummary
output is giving you what it thinks the coefficient ofx
should be given this model. If you really want to force it, then perhaps you should not be includingx
in the model.
– r2evans
Jan 3 at 16:54
In the long run, I'd think a more meaningful formula could bey ~ x + e
.
– r2evans
Jan 3 at 16:55
2
Just a note: in addition, one could also useI(b1*x)
in the model formula as it would then not be interpreted as an interaction term, but as the actual product of these two columns. Not clear though that this is what is the OPs intent.
– coffeinjunky
Jan 3 at 16:58
|
show 1 more comment
I suggest you put everything in a data.frame
and deal with it that way:
set.seed(2)
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
d <- data.frame(y,b0,b1,x,e=rnorm(20,0,1))
head(d)
# y b0 b1 x e
# 1 -0.89691455 0 1 2.090819205 -0.3835862
# 2 0.18484918 0 1 -1.199925820 -1.9591032
# 3 1.58784533 0 1 1.589638200 -0.8417051
# 4 -1.13037567 0 1 1.954651642 1.9035475
# 5 -0.08025176 0 1 0.004937777 0.6224939
# 6 0.13242028 0 1 -2.451706388 1.9909204
Now things work nicely:
modelfit1 <- lm(y~b0+b1*x+e, data=d)
modelfit1
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Coefficients:
# (Intercept) b0 b1 x e b1:x
# 0.19331 NA NA -0.06752 0.02240 NA
summary(modelfit1)
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Residuals:
# Min 1Q Median 3Q Max
# -2.5006 -0.4786 -0.1425 0.6211 1.8488
# Coefficients: (3 not defined because of singularities)
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.19331 0.25013 0.773 0.450
# b0 NA NA NA NA
# b1 NA NA NA NA
# x -0.06752 0.21720 -0.311 0.760
# e 0.02240 0.20069 0.112 0.912
# b1:x NA NA NA NA
# Residual standard error: 1.115 on 17 degrees of freedom
# Multiple R-squared: 0.006657, Adjusted R-squared: -0.1102
# F-statistic: 0.05697 on 2 and 17 DF, p-value: 0.9448
thank you. though i am really confused how the parameters b0 and b1 have NA.
– bel
Jan 3 at 16:39
In this case, they are singular and invariant, so they can have no impact on the model. Realize that the model is already including an intercept ((Intercept)
), which your formula is trying to mimick withb0
; if you want to exclude the automatic intercept in the formula, you'll need to subtract one in the formula, as iny ~ b0 + b1*x + e - 1
, which now gives you a meaningfulb1
but stillb0
is invariant ... which doesn't help your model. If this is for a class, I suggest you go ask the professor about what's happening here.
– r2evans
Jan 3 at 16:53
BTW: addingb1
is also meaningless here, since thex
line in thesummary
output is giving you what it thinks the coefficient ofx
should be given this model. If you really want to force it, then perhaps you should not be includingx
in the model.
– r2evans
Jan 3 at 16:54
In the long run, I'd think a more meaningful formula could bey ~ x + e
.
– r2evans
Jan 3 at 16:55
2
Just a note: in addition, one could also useI(b1*x)
in the model formula as it would then not be interpreted as an interaction term, but as the actual product of these two columns. Not clear though that this is what is the OPs intent.
– coffeinjunky
Jan 3 at 16:58
|
show 1 more comment
I suggest you put everything in a data.frame
and deal with it that way:
set.seed(2)
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
d <- data.frame(y,b0,b1,x,e=rnorm(20,0,1))
head(d)
# y b0 b1 x e
# 1 -0.89691455 0 1 2.090819205 -0.3835862
# 2 0.18484918 0 1 -1.199925820 -1.9591032
# 3 1.58784533 0 1 1.589638200 -0.8417051
# 4 -1.13037567 0 1 1.954651642 1.9035475
# 5 -0.08025176 0 1 0.004937777 0.6224939
# 6 0.13242028 0 1 -2.451706388 1.9909204
Now things work nicely:
modelfit1 <- lm(y~b0+b1*x+e, data=d)
modelfit1
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Coefficients:
# (Intercept) b0 b1 x e b1:x
# 0.19331 NA NA -0.06752 0.02240 NA
summary(modelfit1)
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Residuals:
# Min 1Q Median 3Q Max
# -2.5006 -0.4786 -0.1425 0.6211 1.8488
# Coefficients: (3 not defined because of singularities)
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.19331 0.25013 0.773 0.450
# b0 NA NA NA NA
# b1 NA NA NA NA
# x -0.06752 0.21720 -0.311 0.760
# e 0.02240 0.20069 0.112 0.912
# b1:x NA NA NA NA
# Residual standard error: 1.115 on 17 degrees of freedom
# Multiple R-squared: 0.006657, Adjusted R-squared: -0.1102
# F-statistic: 0.05697 on 2 and 17 DF, p-value: 0.9448
I suggest you put everything in a data.frame
and deal with it that way:
set.seed(2)
m1<-0
sd1<-1
y<-rnorm(n1,m1, sd1)
x<-rnorm(n1,m1, sd1)
b0<-0
b1<-1
d <- data.frame(y,b0,b1,x,e=rnorm(20,0,1))
head(d)
# y b0 b1 x e
# 1 -0.89691455 0 1 2.090819205 -0.3835862
# 2 0.18484918 0 1 -1.199925820 -1.9591032
# 3 1.58784533 0 1 1.589638200 -0.8417051
# 4 -1.13037567 0 1 1.954651642 1.9035475
# 5 -0.08025176 0 1 0.004937777 0.6224939
# 6 0.13242028 0 1 -2.451706388 1.9909204
Now things work nicely:
modelfit1 <- lm(y~b0+b1*x+e, data=d)
modelfit1
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Coefficients:
# (Intercept) b0 b1 x e b1:x
# 0.19331 NA NA -0.06752 0.02240 NA
summary(modelfit1)
# Call:
# lm(formula = y ~ b0 + b1 * x + e, data = d)
# Residuals:
# Min 1Q Median 3Q Max
# -2.5006 -0.4786 -0.1425 0.6211 1.8488
# Coefficients: (3 not defined because of singularities)
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.19331 0.25013 0.773 0.450
# b0 NA NA NA NA
# b1 NA NA NA NA
# x -0.06752 0.21720 -0.311 0.760
# e 0.02240 0.20069 0.112 0.912
# b1:x NA NA NA NA
# Residual standard error: 1.115 on 17 degrees of freedom
# Multiple R-squared: 0.006657, Adjusted R-squared: -0.1102
# F-statistic: 0.05697 on 2 and 17 DF, p-value: 0.9448
answered Jan 3 at 16:28


r2evansr2evans
28.5k33259
28.5k33259
thank you. though i am really confused how the parameters b0 and b1 have NA.
– bel
Jan 3 at 16:39
In this case, they are singular and invariant, so they can have no impact on the model. Realize that the model is already including an intercept ((Intercept)
), which your formula is trying to mimick withb0
; if you want to exclude the automatic intercept in the formula, you'll need to subtract one in the formula, as iny ~ b0 + b1*x + e - 1
, which now gives you a meaningfulb1
but stillb0
is invariant ... which doesn't help your model. If this is for a class, I suggest you go ask the professor about what's happening here.
– r2evans
Jan 3 at 16:53
BTW: addingb1
is also meaningless here, since thex
line in thesummary
output is giving you what it thinks the coefficient ofx
should be given this model. If you really want to force it, then perhaps you should not be includingx
in the model.
– r2evans
Jan 3 at 16:54
In the long run, I'd think a more meaningful formula could bey ~ x + e
.
– r2evans
Jan 3 at 16:55
2
Just a note: in addition, one could also useI(b1*x)
in the model formula as it would then not be interpreted as an interaction term, but as the actual product of these two columns. Not clear though that this is what is the OPs intent.
– coffeinjunky
Jan 3 at 16:58
|
show 1 more comment
thank you. though i am really confused how the parameters b0 and b1 have NA.
– bel
Jan 3 at 16:39
In this case, they are singular and invariant, so they can have no impact on the model. Realize that the model is already including an intercept ((Intercept)
), which your formula is trying to mimick withb0
; if you want to exclude the automatic intercept in the formula, you'll need to subtract one in the formula, as iny ~ b0 + b1*x + e - 1
, which now gives you a meaningfulb1
but stillb0
is invariant ... which doesn't help your model. If this is for a class, I suggest you go ask the professor about what's happening here.
– r2evans
Jan 3 at 16:53
BTW: addingb1
is also meaningless here, since thex
line in thesummary
output is giving you what it thinks the coefficient ofx
should be given this model. If you really want to force it, then perhaps you should not be includingx
in the model.
– r2evans
Jan 3 at 16:54
In the long run, I'd think a more meaningful formula could bey ~ x + e
.
– r2evans
Jan 3 at 16:55
2
Just a note: in addition, one could also useI(b1*x)
in the model formula as it would then not be interpreted as an interaction term, but as the actual product of these two columns. Not clear though that this is what is the OPs intent.
– coffeinjunky
Jan 3 at 16:58
thank you. though i am really confused how the parameters b0 and b1 have NA.
– bel
Jan 3 at 16:39
thank you. though i am really confused how the parameters b0 and b1 have NA.
– bel
Jan 3 at 16:39
In this case, they are singular and invariant, so they can have no impact on the model. Realize that the model is already including an intercept (
(Intercept)
), which your formula is trying to mimick with b0
; if you want to exclude the automatic intercept in the formula, you'll need to subtract one in the formula, as in y ~ b0 + b1*x + e - 1
, which now gives you a meaningful b1
but still b0
is invariant ... which doesn't help your model. If this is for a class, I suggest you go ask the professor about what's happening here.– r2evans
Jan 3 at 16:53
In this case, they are singular and invariant, so they can have no impact on the model. Realize that the model is already including an intercept (
(Intercept)
), which your formula is trying to mimick with b0
; if you want to exclude the automatic intercept in the formula, you'll need to subtract one in the formula, as in y ~ b0 + b1*x + e - 1
, which now gives you a meaningful b1
but still b0
is invariant ... which doesn't help your model. If this is for a class, I suggest you go ask the professor about what's happening here.– r2evans
Jan 3 at 16:53
BTW: adding
b1
is also meaningless here, since the x
line in the summary
output is giving you what it thinks the coefficient of x
should be given this model. If you really want to force it, then perhaps you should not be including x
in the model.– r2evans
Jan 3 at 16:54
BTW: adding
b1
is also meaningless here, since the x
line in the summary
output is giving you what it thinks the coefficient of x
should be given this model. If you really want to force it, then perhaps you should not be including x
in the model.– r2evans
Jan 3 at 16:54
In the long run, I'd think a more meaningful formula could be
y ~ x + e
.– r2evans
Jan 3 at 16:55
In the long run, I'd think a more meaningful formula could be
y ~ x + e
.– r2evans
Jan 3 at 16:55
2
2
Just a note: in addition, one could also use
I(b1*x)
in the model formula as it would then not be interpreted as an interaction term, but as the actual product of these two columns. Not clear though that this is what is the OPs intent.– coffeinjunky
Jan 3 at 16:58
Just a note: in addition, one could also use
I(b1*x)
in the model formula as it would then not be interpreted as an interaction term, but as the actual product of these two columns. Not clear though that this is what is the OPs intent.– coffeinjunky
Jan 3 at 16:58
|
show 1 more comment
I may be wrong, but I believe you want to simulate an outcome and then estimate it's parameters. If that is true, you would rather do the following:
n1 <- 20
m1 <- 0
sd1<- 1
b0 <- 0
b1 <- 1
x <- rnorm(n1,m1, sd1)
e <- rnorm(n1,m1, sd1)
y <- b0 + b1*x + e
summary(lm(y~x))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.66052 -0.40203 0.05659 0.44115 1.38798
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.3078 0.1951 -1.578 0.132
x 1.1774 0.2292 5.137 6.9e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.852 on 18 degrees of freedom
Multiple R-squared: 0.5945, Adjusted R-squared: 0.572
F-statistic: 26.39 on 1 and 18 DF, p-value: 6.903e-05
And in case you want to do this multiple times, consider the following:
repetitions <- 5
betas <- t(sapply(1:repetitions, function(i){
y <- b0 + b1*x + rnorm(n1,m1, sd1)
coefficients(lm(y~x))
}))
betas
(Intercept) x
[1,] 0.21989182 0.8185690
[2,] -0.12820726 0.7289041
[3,] -0.27596844 0.9794432
[4,] 0.06145306 1.0575050
[5,] -0.31429950 0.9984262
Now you can look at the mean of the estimated betas:
colMeans(betas)
(Intercept) x
-0.08742606 0.91656951
and the variance-covariance matrix:
var(betas)
(Intercept) x
(Intercept) 0.051323041 -0.007976803
x -0.007976803 0.018834711
add a comment |
I may be wrong, but I believe you want to simulate an outcome and then estimate it's parameters. If that is true, you would rather do the following:
n1 <- 20
m1 <- 0
sd1<- 1
b0 <- 0
b1 <- 1
x <- rnorm(n1,m1, sd1)
e <- rnorm(n1,m1, sd1)
y <- b0 + b1*x + e
summary(lm(y~x))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.66052 -0.40203 0.05659 0.44115 1.38798
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.3078 0.1951 -1.578 0.132
x 1.1774 0.2292 5.137 6.9e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.852 on 18 degrees of freedom
Multiple R-squared: 0.5945, Adjusted R-squared: 0.572
F-statistic: 26.39 on 1 and 18 DF, p-value: 6.903e-05
And in case you want to do this multiple times, consider the following:
repetitions <- 5
betas <- t(sapply(1:repetitions, function(i){
y <- b0 + b1*x + rnorm(n1,m1, sd1)
coefficients(lm(y~x))
}))
betas
(Intercept) x
[1,] 0.21989182 0.8185690
[2,] -0.12820726 0.7289041
[3,] -0.27596844 0.9794432
[4,] 0.06145306 1.0575050
[5,] -0.31429950 0.9984262
Now you can look at the mean of the estimated betas:
colMeans(betas)
(Intercept) x
-0.08742606 0.91656951
and the variance-covariance matrix:
var(betas)
(Intercept) x
(Intercept) 0.051323041 -0.007976803
x -0.007976803 0.018834711
add a comment |
I may be wrong, but I believe you want to simulate an outcome and then estimate it's parameters. If that is true, you would rather do the following:
n1 <- 20
m1 <- 0
sd1<- 1
b0 <- 0
b1 <- 1
x <- rnorm(n1,m1, sd1)
e <- rnorm(n1,m1, sd1)
y <- b0 + b1*x + e
summary(lm(y~x))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.66052 -0.40203 0.05659 0.44115 1.38798
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.3078 0.1951 -1.578 0.132
x 1.1774 0.2292 5.137 6.9e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.852 on 18 degrees of freedom
Multiple R-squared: 0.5945, Adjusted R-squared: 0.572
F-statistic: 26.39 on 1 and 18 DF, p-value: 6.903e-05
And in case you want to do this multiple times, consider the following:
repetitions <- 5
betas <- t(sapply(1:repetitions, function(i){
y <- b0 + b1*x + rnorm(n1,m1, sd1)
coefficients(lm(y~x))
}))
betas
(Intercept) x
[1,] 0.21989182 0.8185690
[2,] -0.12820726 0.7289041
[3,] -0.27596844 0.9794432
[4,] 0.06145306 1.0575050
[5,] -0.31429950 0.9984262
Now you can look at the mean of the estimated betas:
colMeans(betas)
(Intercept) x
-0.08742606 0.91656951
and the variance-covariance matrix:
var(betas)
(Intercept) x
(Intercept) 0.051323041 -0.007976803
x -0.007976803 0.018834711
I may be wrong, but I believe you want to simulate an outcome and then estimate it's parameters. If that is true, you would rather do the following:
n1 <- 20
m1 <- 0
sd1<- 1
b0 <- 0
b1 <- 1
x <- rnorm(n1,m1, sd1)
e <- rnorm(n1,m1, sd1)
y <- b0 + b1*x + e
summary(lm(y~x))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.66052 -0.40203 0.05659 0.44115 1.38798
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.3078 0.1951 -1.578 0.132
x 1.1774 0.2292 5.137 6.9e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.852 on 18 degrees of freedom
Multiple R-squared: 0.5945, Adjusted R-squared: 0.572
F-statistic: 26.39 on 1 and 18 DF, p-value: 6.903e-05
And in case you want to do this multiple times, consider the following:
repetitions <- 5
betas <- t(sapply(1:repetitions, function(i){
y <- b0 + b1*x + rnorm(n1,m1, sd1)
coefficients(lm(y~x))
}))
betas
(Intercept) x
[1,] 0.21989182 0.8185690
[2,] -0.12820726 0.7289041
[3,] -0.27596844 0.9794432
[4,] 0.06145306 1.0575050
[5,] -0.31429950 0.9984262
Now you can look at the mean of the estimated betas:
colMeans(betas)
(Intercept) x
-0.08742606 0.91656951
and the variance-covariance matrix:
var(betas)
(Intercept) x
(Intercept) 0.051323041 -0.007976803
x -0.007976803 0.018834711
edited Jan 3 at 17:01
answered Jan 3 at 16:41


coffeinjunkycoffeinjunky
7,4142143
7,4142143
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025722%2fr-lm-parameter-estimates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What do you want to do?
– coffeinjunky
Jan 3 at 16:00
1
Hello, welcome to StackOverflow. I think your simulated example does not really match what you want to do. Maybe this post would help? stats.stackexchange.com/questions/99924/…
– Vincent Guillemot
Jan 3 at 16:01
1
A comment on where the error comes from: R thinks that the model you want to fit contains a response variable y (so far so good), and a complicated combination of explanatory variables (one called b0, one called b1, one called x, one interaction term between the variables x and b1 and a final term that I don't even know how R will interpret it). If your intention was to generate a simple dataset to fit a simple linear model on (and I think it was your intention), then you need to fix your simulated data.
– Vincent Guillemot
Jan 3 at 16:10
@coffeinjunky hello, i want to see the estimate standard error of the parameter b0 and b1.
– bel
Jan 3 at 16:28