0

I have only very basic R skills. I got stuck at one point in the chapter 2.8.6 from the R book from Michael Crowley

The code supplied is:

sapdecay <- read.table("c:\\temp\\sapdecay.txt",header=T)
attach(sapdecay)
names(sapdecay)
sumsq <- function(a,xv=x,yv=y)
{ yf <- exp(-a*xv)
  sum((yv-yf)^2) }

sumsq
lm(log(y)~x)
lm

But after I type this:

lm(log(y)~x)

I get this:

Error in model.frame.default(formula = log(y) ~ x, drop.unused.levels = TRUE) : 
  variable lengths differ (found for 'x')
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Welcome to SO, Sebastian! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically after `set.seed(1)`), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 31 '20 at 17:34
  • Further, while I understand you're learning R and the tutorial has you doing this, I discourage the use of `attach` in just about every use. (Said differently: I do not know of a situation where its use is safe, advisable, and worth the risk that it brings. And potentially-sloppy programming that it encourages.) – r2evans Jul 31 '20 at 17:35

2 Answers2

0

As r2evans mentions in a comment

Simply omitting attach() fixed the problem.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

I cannot replicate your error. I found the data on the internet on Crawley's webpage for the book sapdecay.txt. Loading it and using dput puts in in a simple form for us to work with:

sapdecay <- structure(list(x = c(0L, 2L, 4L, 6L, 8L, 10L, 12L, 14L, 16L, 
18L, 20L, 22L, 24L, 26L, 28L, 30L, 32L, 34L, 36L, 38L, 40L, 42L, 
44L, 46L, 48L, 50L), y = c(1, 0.960235403, 0.844663774, 0.706936329, 
0.7086414, 0.609795365, 0.513252644, 0.471399685, 0.384081274, 
0.372277798, 0.334087432, 0.273307329, 0.25162333, 0.222786515, 
0.210216526, 0.190591821, 0.166907195, 0.140731643, 0.12028785, 
0.116460713, 0.092736872, 0.083972607, 0.088291404, 0.071790589, 
0.059327414, 0.061706234)), class = "data.frame", row.names = c(NA, 
-26L))

I agree with the recommendation to avoid attach:

lm(log(y)~x, sapdecay)
# 
# Call:
# lm(formula = log(y) ~ x, data = sapdecay)
# 
# Coefficients:
# (Intercept)            x  
#    0.04688     -0.05849  

You may already have variables named x and/or y that are conflicting with the ones in sapdecay.

dcarlson
  • 10,936
  • 2
  • 15
  • 18